
<!doctype html>
<html lang="zh" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
        <link rel="canonical" href="https://github.tim-wcx.ltd/mkdocs/course/make/">
      
      
        <link rel="prev" href="../PDF%E5%88%92%E8%AF%8D%E7%BF%BB%E8%AF%91/">
      
      
        <link rel="next" href="../RT_Smart%E4%B8%ADminizip%E7%A7%BB%E6%A4%8D%E8%AE%B0%E5%BD%95/">
      
      <link rel="icon" href="../../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.4.3, mkdocs-material-9.1.13">
    
    
      
        <title>makefile学习笔记 - Tim-Wcx</title>
      
    
    
      <link rel="stylesheet" href="../../assets/stylesheets/main.85bb2934.min.css">
      
        
        <link rel="stylesheet" href="../../assets/stylesheets/palette.a6bdf11c.min.css">
      
      

    
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
    <script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="indigo" data-md-color-accent="cyan">
  
    
    
      <script>var palette=__md_get("__palette");if(palette&&"object"==typeof palette.color)for(var key of Object.keys(palette.color))document.body.setAttribute("data-md-color-"+key,palette.color[key])</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#gcc" class="md-skip">
          跳转至
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
    
      

<header class="md-header" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="页眉">
    <a href="../.." title="Tim-Wcx" class="md-header__button md-logo" aria-label="Tim-Wcx" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19.5 9V1.5h-3V9h-3V1.5h-3V9h-3V1.5H4.65V9H3v1.5h18V9h-1.5m0 4.5h-3V21h-3v-7.5h-3V21h-3v-7.5H4.65V21H3v1.5h18V21h-1.5v-7.5Z"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            Tim-Wcx
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              makefile学习笔记
            
          </span>
        </div>
      </div>
    </div>
    
      
        <form class="md-header__option" data-md-component="palette">
          
            
            
            
            <input class="md-option" data-md-color-media="" data-md-color-scheme="default" data-md-color-primary="indigo" data-md-color-accent="cyan"  aria-label="切换至夜间模式"  type="radio" name="__palette" id="__palette_1">
            
              <label class="md-header__button md-icon" title="切换至夜间模式" for="__palette_2" hidden>
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m17.75 4.09-2.53 1.94.91 3.06-2.63-1.81-2.63 1.81.91-3.06-2.53-1.94L12.44 4l1.06-3 1.06 3 3.19.09m3.5 6.91-1.64 1.25.59 1.98-1.7-1.17-1.7 1.17.59-1.98L15.75 11l2.06-.05L18.5 9l.69 1.95 2.06.05m-2.28 4.95c.83-.08 1.72 1.1 1.19 1.85-.32.45-.66.87-1.08 1.27C15.17 23 8.84 23 4.94 19.07c-3.91-3.9-3.91-10.24 0-14.14.4-.4.82-.76 1.27-1.08.75-.53 1.93.36 1.85 1.19-.27 2.86.69 5.83 2.89 8.02a9.96 9.96 0 0 0 8.02 2.89m-1.64 2.02a12.08 12.08 0 0 1-7.8-3.47c-2.17-2.19-3.33-5-3.49-7.82-2.81 3.14-2.7 7.96.31 10.98 3.02 3.01 7.84 3.12 10.98.31Z"/></svg>
              </label>
            
          
            
            
            
            <input class="md-option" data-md-color-media="" data-md-color-scheme="slate" data-md-color-primary="black" data-md-color-accent="cyan"  aria-label="切换至日间模式"  type="radio" name="__palette" id="__palette_2">
            
              <label class="md-header__button md-icon" title="切换至日间模式" for="__palette_1" hidden>
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 7a5 5 0 0 1 5 5 5 5 0 0 1-5 5 5 5 0 0 1-5-5 5 5 0 0 1 5-5m0 2a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0-7 2.39 3.42C13.65 5.15 12.84 5 12 5c-.84 0-1.65.15-2.39.42L12 2M3.34 7l4.16-.35A7.2 7.2 0 0 0 5.94 8.5c-.44.74-.69 1.5-.83 2.29L3.34 7m.02 10 1.76-3.77a7.131 7.131 0 0 0 2.38 4.14L3.36 17M20.65 7l-1.77 3.79a7.023 7.023 0 0 0-2.38-4.15l4.15.36m-.01 10-4.14.36c.59-.51 1.12-1.14 1.54-1.86.42-.73.69-1.5.83-2.29L20.64 17M12 22l-2.41-3.44c.74.27 1.55.44 2.41.44.82 0 1.63-.17 2.37-.44L12 22Z"/></svg>
              </label>
            
          
        </form>
      
    
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
      </label>
      <div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="搜索" placeholder="搜索" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="查找">
        
        <button type="reset" class="md-search__icon md-icon" title="清空当前内容" aria-label="清空当前内容" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg>
        </button>
      </nav>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            正在初始化搜索引擎
          </div>
          <ol class="md-search-result__list" role="presentation"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
            
<nav class="md-tabs" aria-label="标签" data-md-component="tabs">
  <div class="md-grid">
    <ul class="md-tabs__list">
      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../problem/%E7%BB%84%E9%98%9F%E8%AE%AD%E7%BB%83%E8%B5%9B%EF%BC%88%E5%9B%9B%EF%BC%89/" class="md-tabs__link">
        ICPC题解
      </a>
    </li>
  

      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../icpc/%E4%B8%AA%E4%BA%BA%E6%A8%A1%E6%9D%BF/" class="md-tabs__link">
        ICPC模板
      </a>
    </li>
  

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../question/" class="md-tabs__link">
      ICPC题单
    </a>
  </li>

      
        
  
  
    
  


  
  
  
    <li class="md-tabs__item">
      <a href="../%E5%9B%BE%E4%BC%98%E7%AE%97%E6%B3%95%E7%AC%94%E8%AE%B0/" class="md-tabs__link md-tabs__link--active">
        课程复习
      </a>
    </li>
  

      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../csdn/2020-08-01/" class="md-tabs__link">
        csdn导出
      </a>
    </li>
  

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../plan/" class="md-tabs__link">
      个人计划
    </a>
  </li>

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../about/" class="md-tabs__link">
      关于
    </a>
  </li>

      
    </ul>
  </div>
</nav>
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

  


<nav class="md-nav md-nav--primary md-nav--lifted" aria-label="导航栏" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../.." title="Tim-Wcx" class="md-nav__button md-logo" aria-label="Tim-Wcx" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19.5 9V1.5h-3V9h-3V1.5h-3V9h-3V1.5H4.65V9H3v1.5h18V9h-1.5m0 4.5h-3V21h-3v-7.5h-3V21h-3v-7.5H4.65V21H3v1.5h18V21h-1.5v-7.5Z"/></svg>

    </a>
    Tim-Wcx
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_1" >
      
      
      
        <label class="md-nav__link" for="__nav_1" id="__nav_1_label" tabindex="0">
          ICPC题解
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_1_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_1">
          <span class="md-nav__icon md-icon"></span>
          ICPC题解
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E7%BB%84%E9%98%9F%E8%AE%AD%E7%BB%83%E8%B5%9B%EF%BC%88%E5%9B%9B%EF%BC%89/" class="md-nav__link">
        组队训练赛（四）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E7%BB%84%E9%98%9F%E8%AE%AD%E7%BB%83%E8%B5%9B%EF%BC%88%E4%B8%89%EF%BC%89/" class="md-nav__link">
        组队训练赛（三）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E7%BB%84%E9%98%9F%E8%AE%AD%E7%BB%83%E8%B5%9B%EF%BC%88%E4%BA%8C%EF%BC%89/" class="md-nav__link">
        组队训练赛（二）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E7%BB%84%E9%98%9F%E8%AE%AD%E7%BB%83%E8%B5%9B%EF%BC%88%E4%B8%80%EF%BC%89/" class="md-nav__link">
        组队训练赛（一）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E5%8D%81%E4%BA%94%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（十五）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E5%8D%81%E5%9B%9B%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（十四）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E5%8D%81%E4%B8%89%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（十三）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E5%8D%81%E4%BA%8C%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（十二）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E5%8D%81%E4%B8%80%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（十一）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E5%8D%81%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（十）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E4%B9%9D%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（九）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E5%85%AB%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（八）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E4%B8%83%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（七）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E5%85%AD%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（六）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E4%BA%94%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（五）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E5%9B%9B%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（四）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E4%B8%89%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（三）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E7%B3%BB%E5%88%97%E9%A2%98%E8%A7%A3%EF%BC%88%E4%BA%8C%EF%BC%89/" class="md-nav__link">
        暑假集训系列题解（二）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/%E6%9A%91%E5%81%87%E9%9B%86%E8%AE%AD%E9%A2%98%E8%A7%A3%E7%B3%BB%E5%88%97%EF%BC%88%E4%B8%80%EF%BC%89/" class="md-nav__link">
        暑假集训题解系列（一）
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-02-2%E6%9C%882%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-02-2月2日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-07-2%E6%9C%887%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-07-2月7日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-02-2%E6%9C%882%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-02-2月2日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-07-2%E6%9C%887%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-07-2月7日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-08-2%E6%9C%888%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-08-2月8日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-09-2%E6%9C%889%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-09-2月9日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-12-%E5%AE%89%E5%8D%93%E8%AF%BE%E8%A1%A8%E5%AE%A2%E6%88%B7%E7%AB%AF%2B%E6%9C%8D%E5%8A%A1%E7%AB%AF%E6%B3%A8%E8%A7%A3/" class="md-nav__link">
        2021-02-12-安卓课表客户端+服务端注解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-16-2%E6%9C%8816%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-16-2月16日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-19-2%E6%9C%8819%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-19-2月19日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-20-2%E6%9C%8820%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-20-2月20日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-22-2%E6%9C%8822%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-22-2月22日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-24-2%E6%9C%8824%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-24-2月24日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-02-28-2%E6%9C%8828%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-02-28-2月28日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-03-02-3%E6%9C%882%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-03-02-3月2日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-03-07-3%E6%9C%887%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-03-07-3月7日题解
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-03-09-%E6%95%B0%E8%AE%BA/" class="md-nav__link">
        2021-03-09-数论
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../problem/2021-06-05-6%E6%9C%885%E6%97%A5%E9%A2%98%E8%A7%A3/" class="md-nav__link">
        2021-06-05-6月5日题解
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_2" >
      
      
      
        <label class="md-nav__link" for="__nav_2" id="__nav_2_label" tabindex="0">
          ICPC模板
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_2_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_2">
          <span class="md-nav__icon md-icon"></span>
          ICPC模板
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../icpc/%E4%B8%AA%E4%BA%BA%E6%A8%A1%E6%9D%BF/" class="md-nav__link">
        个人模板
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../icpc/kuangbin%E6%A8%A1%E6%9D%BF/" class="md-nav__link">
        kuangbin模板
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../question/" class="md-nav__link">
        ICPC题单
      </a>
    </li>
  

    
      
      
      

  
  
    
  
  
    
    <li class="md-nav__item md-nav__item--active md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_4" checked>
      
      
      
        <label class="md-nav__link" for="__nav_4" id="__nav_4_label" tabindex="0">
          课程复习
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_4_label" aria-expanded="true">
        <label class="md-nav__title" for="__nav_4">
          <span class="md-nav__icon md-icon"></span>
          课程复习
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E5%9B%BE%E4%BC%98%E7%AE%97%E6%B3%95%E7%AC%94%E8%AE%B0/" class="md-nav__link">
        图优算法笔记
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E5%8D%9A%E6%B5%81WIFI%E8%B5%84%E6%96%99/" class="md-nav__link">
        博流WIFI资料
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%95%B0%E5%AD%97%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86/" class="md-nav__link">
        数字图像处理
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E7%8E%B0%E4%BB%A3%E5%AF%86%E7%A0%81%E5%AD%A6/" class="md-nav__link">
        现代密码学
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E8%BD%AF%E4%BB%B6%E5%B7%A5%E7%A8%8B/" class="md-nav__link">
        软件工程
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9B%BE%E5%BD%A2%E5%AD%A6/" class="md-nav__link">
        计算机图形学
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E8%AE%A1%E7%AE%97%E6%9C%BA%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" class="md-nav__link">
        计算机操作系统
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E5%8D%95%E7%89%87%E6%9C%BA/" class="md-nav__link">
        单片机
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../PDF%E5%88%92%E8%AF%8D%E7%BF%BB%E8%AF%91/" class="md-nav__link">
        PDF划词翻译
      </a>
    </li>
  

            
          
            
              
  
  
    
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
      
      
        
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          makefile学习笔记
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="./" class="md-nav__link md-nav__link--active">
        makefile学习笔记
      </a>
      
        

<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#options" class="md-nav__link">
    常用选项(options)
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    静态链接库
  </a>
  
    <nav class="md-nav" aria-label="静态链接库">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    命名
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    归档（静态链接库生成）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    使用
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_5" class="md-nav__link">
    动态链接库
  </a>
  
    <nav class="md-nav" aria-label="动态链接库">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_6" class="md-nav__link">
    命名
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_7" class="md-nav__link">
    生成
  </a>
  
    <nav class="md-nav" aria-label="生成">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_8" class="md-nav__link">
    使用
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../RT_Smart%E4%B8%ADminizip%E7%A7%BB%E6%A4%8D%E8%AE%B0%E5%BD%95/" class="md-nav__link">
        RT_Smart中minizip移植记录
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_5" >
      
      
      
        <label class="md-nav__link" for="__nav_5" id="__nav_5_label" tabindex="0">
          csdn导出
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_5_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_5">
          <span class="md-nav__icon md-icon"></span>
          csdn导出
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-01/" class="md-nav__link">
        2020-08-01
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-02%E7%BA%BF%E6%AE%B5%E6%A0%91/" class="md-nav__link">
        2020-08-02线段树
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-03/" class="md-nav__link">
        2020-08-03
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-04/" class="md-nav__link">
        2020-08-04
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-05/" class="md-nav__link">
        2020-08-05
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-06%E7%BA%BF%E6%AE%B5%E6%A0%91/" class="md-nav__link">
        2020-08-06线段树
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-07/" class="md-nav__link">
        2020-08-07
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-08/" class="md-nav__link">
        2020-08-08
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-09/" class="md-nav__link">
        2020-08-09
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-10/" class="md-nav__link">
        2020-08-10
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-11/" class="md-nav__link">
        2020-08-11
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-12%E6%AC%A7%E6%8B%89%E5%87%BD%E6%95%B0/" class="md-nav__link">
        2020-08-12欧拉函数
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-13/" class="md-nav__link">
        2020-08-13
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-14/" class="md-nav__link">
        2020-08-14
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-16%E6%A0%91%E5%BD%A2DP%E7%AD%89%E5%B7%AE%E6%95%B0%E5%88%97%E5%89%8D%E7%BC%80%E5%92%8C/" class="md-nav__link">
        2020-08-16树形DP等差数列前缀和
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-17/" class="md-nav__link">
        2020-08-17
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-18/" class="md-nav__link">
        2020-08-18
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-19/" class="md-nav__link">
        2020-08-19
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-20/" class="md-nav__link">
        2020-08-20
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-08-21/" class="md-nav__link">
        2020-08-21
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-09-05%E5%88%92%E5%88%86%E6%A0%91%E6%B1%82%E6%9F%90%E4%B8%80%E5%8C%BA%E9%97%B4%E7%9A%84%E4%B8%AD%E4%BD%8D%E6%95%B0/" class="md-nav__link">
        2020-09-05划分树求某一区间的中位数
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-09-07/" class="md-nav__link">
        2020-09-07
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-09-08%E4%BA%8C%E5%88%86%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91/" class="md-nav__link">
        2020-09-08二分最小生成树
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-09-08%E6%95%B0%E4%BD%8DDP%E4%BA%8C%E5%88%86/" class="md-nav__link">
        2020-09-08数位DP二分
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-09-09%E6%95%B0%E4%BD%8DDP/" class="md-nav__link">
        2020-09-09数位DP
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-09-11%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84%E6%B1%82%E6%9C%80%E5%A4%A7%E5%80%BC/" class="md-nav__link">
        2020-09-11树状数组求最大值
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-09-12%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%93%88%E5%B8%8C/" class="md-nav__link">
        2020-09-12字符串哈希
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-09-12/" class="md-nav__link">
        2020-09-12
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-09-21%E5%87%B8%E5%8C%85%E7%AE%97%E6%B3%95/" class="md-nav__link">
        2020-09-21凸包算法
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-09-23%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84/" class="md-nav__link">
        2020-09-23树状数组
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-09-26%E6%9C%80%E5%B0%8F%E8%B4%B9%E7%94%A8%E6%9C%80%E5%A4%A7%E6%B5%81%E9%97%AE%E9%A2%98/" class="md-nav__link">
        2020-09-26最小费用最大流问题
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-10-04%E7%BA%BF%E6%AE%B5%E6%A0%91%E6%89%AB%E6%8F%8F%E7%BA%BF/" class="md-nav__link">
        2020-10-04线段树扫描线
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-10-05/" class="md-nav__link">
        2020-10-05
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-10-07%E5%B9%B6%E6%9F%A5%E9%9B%86%E5%90%AF%E5%8F%91%E5%BC%8F%E5%90%88%E5%B9%B6/" class="md-nav__link">
        2020-10-07并查集启发式合并
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-10-15dsuontree/" class="md-nav__link">
        2020-10-15dsuontree
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-11-23/" class="md-nav__link">
        2020-11-23
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-12-02/" class="md-nav__link">
        2020-12-02
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2020-7-31/" class="md-nav__link">
        2020-7-31
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2021-02-02/" class="md-nav__link">
        2021-02-02
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/2021-02-24/" class="md-nav__link">
        2021-02-24
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/Beyond_compare4_30%E5%A4%A9%E8%AF%95%E7%94%A8%E8%BF%87%E6%9C%9F/" class="md-nav__link">
        Beyondcompare430天试用过期
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/GitHub%E5%8D%9A%E5%AE%A2/" class="md-nav__link">
        GitHub博客
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/%E4%BA%8C%E5%88%86%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%E6%A8%A1%E6%9D%BF/" class="md-nav__link">
        二分二分查找模板
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/%E5%BC%82%E6%88%96%E6%B1%82%E5%92%8C%E5%BC%8F/" class="md-nav__link">
        异或求和式
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/%E6%B1%82%E7%BB%84%E5%90%88%E6%95%B0%E7%9A%84%E6%96%B9%E6%B3%95/" class="md-nav__link">
        求组合数的方法
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/%E6%B1%82%E9%80%86%E5%BA%8F%E5%AF%B9/" class="md-nav__link">
        求逆序对
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/%E7%AE%80%E4%BB%8B%E6%89%A9%E5%B1%95%E6%AC%A7%E5%87%A0%E9%87%8C%E5%BE%B7%E5%AE%9A%E7%90%86/" class="md-nav__link">
        简介扩展欧几里德定理
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../csdn/%E9%93%BE%E8%A1%A8/" class="md-nav__link">
        链表
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../plan/" class="md-nav__link">
        个人计划
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../about/" class="md-nav__link">
        关于
      </a>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#options" class="md-nav__link">
    常用选项(options)
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    静态链接库
  </a>
  
    <nav class="md-nav" aria-label="静态链接库">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    命名
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    归档（静态链接库生成）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    使用
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_5" class="md-nav__link">
    动态链接库
  </a>
  
    <nav class="md-nav" aria-label="动态链接库">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_6" class="md-nav__link">
    命名
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_7" class="md-nav__link">
    生成
  </a>
  
    <nav class="md-nav" aria-label="生成">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_8" class="md-nav__link">
    使用
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          
            <div class="md-content" data-md-component="content">
              <article class="md-content__inner md-typeset">
                
                  


<h1 id="gcc">GCC用法<a class="headerlink" href="#gcc" title="Permanent link">&para;</a></h1>
<div class="language-text highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a>gcc(g++) &lt;options&gt; &lt;sourcefiles...&gt;
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a># 示例
</span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a>gcc main.cpp
</span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a>gcc -o main main.cpp
</span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a>gcc -o main main.cpp hello.cpp fun.cpp
</span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a>gcc -I../headers -o main main.cpp
</span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a>g++ -o main -I../headers -L../lib -lfun1 main.cpp hello.cpp
</span></code></pre></div>
<h3 id="options">常用选项(options)<a class="headerlink" href="#options" title="Permanent link">&para;</a></h3>
<table>
<thead>
<tr>
<th align="center">选项</th>
<th align="center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">-o <filename></td>
<td align="center">指定生成文件名</td>
</tr>
<tr>
<td align="center">-I<dir></td>
<td align="center">指定头文件搜索路径</td>
</tr>
<tr>
<td align="center">-L<dir></td>
<td align="center">指定库搜索路径</td>
</tr>
<tr>
<td align="center">-static</td>
<td align="center">静态链接</td>
</tr>
<tr>
<td align="center">-E -o<filename.i></td>
<td align="center">编译过程进行到预处理</td>
</tr>
<tr>
<td align="center">-S</td>
<td align="center">编译过程进行到编译</td>
</tr>
<tr>
<td align="center">-c</td>
<td align="center">编译过程进行到汇编</td>
</tr>
<tr>
<td align="center">-O(0-3)</td>
<td align="center">指定代码优化级别</td>
</tr>
<tr>
<td align="center">-Wall</td>
<td align="center">输出警告信息</td>
</tr>
<tr>
<td align="center">-fexec-charset=GBK</td>
<td align="center">指定运行时编码</td>
</tr>
<tr>
<td align="center">-finput-charset=UTF-8</td>
<td align="center">指定源文件编码</td>
</tr>
<tr>
<td align="center">-l<libname></td>
<td align="center">指定链接库名</td>
</tr>
<tr>
<td align="center"></td>
<td align="center"></td>
</tr>
<tr>
<td align="center"></td>
<td align="center"></td>
</tr>
</tbody>
</table>
<h3 id="_1">静态链接库<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3>
<h4 id="_2">命名<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h4>
<p>静态链接库命名采用**lib<name>.a**的形式，例如libmangle.a，libpng.a</p>
<h4 id="_3">归档（静态链接库生成）<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h4>
<p>先将需要归档到库的源文件编译成.o文件，再用下列命令归档</p>
<div class="language-text highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a>ar rcs &lt;libname&gt; &lt;objfilelist...&gt;
</span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a># 示例
</span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a>ar rcs libfun.a fun1.o fun2.o fun3.o # 将3个.o文件归档到同一库文件当中
</span></code></pre></div>
<h4 id="_4">使用<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h4>
<p>先将使用到对应静态库的源文件编译为.o文件，再进行链接</p>
<div class="language-text highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a>gcc(g++) &lt;ofilelist...&gt; -L&lt;dir&gt; -l&lt;lib&gt;
</span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a># 示例
</span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a>gcc main.o hello.o -L../lib -lfun1 -lfun2 -lfun3
</span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a># 或者
</span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a>gcc main.o hello.o ../lib/libfun1.a ../lib/libfun2.a ../lib/libfun3.a
</span></code></pre></div>
<h3 id="_5">动态链接库<a class="headerlink" href="#_5" title="Permanent link">&para;</a></h3>
<h4 id="_6">命名<a class="headerlink" href="#_6" title="Permanent link">&para;</a></h4>
<p>动态链接库命名规则为lib<name>.dll (Windows) / lib<name>.so (Linux)，例如：libmangle.dll/libmangle.so，libpng.dll/libpng.so</p>
<h3 id="_7">生成<a class="headerlink" href="#_7" title="Permanent link">&para;</a></h3>
<div class="language-text highlight"><pre><span></span><code><span id="__span-3-1"><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a># 分步进行
</span><span id="__span-3-2"><a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a>gcc(g++) -c -fPIC &lt;sourcefilelist ...&gt;
</span><span id="__span-3-3"><a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a>gcc(g++) -o &lt;libname&gt; -shared &lt;objfilelist ...&gt;
</span><span id="__span-3-4"><a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a># 示例
</span><span id="__span-3-5"><a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a>gcc -c -fPIC fun1.c fun2.c fun3.c
</span><span id="__span-3-6"><a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a>gcc -o libfun.dll -shared fun1.o fun2.o fun3.o
</span><span id="__span-3-7"><a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a>
</span><span id="__span-3-8"><a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a># 一步到位
</span><span id="__span-3-9"><a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a>gcc(g++) -o &lt;libname&gt; -fPIC -shared &lt;sourcefilelist ...&gt;
</span><span id="__span-3-10"><a id="__codelineno-3-10" name="__codelineno-3-10" href="#__codelineno-3-10"></a># 示例
</span><span id="__span-3-11"><a id="__codelineno-3-11" name="__codelineno-3-11" href="#__codelineno-3-11"></a>gcc -o libfun.dll -fPIC  -shared fun1.c fun2.c fun3.c
</span></code></pre></div>
<h4 id="_8">使用<a class="headerlink" href="#_8" title="Permanent link">&para;</a></h4>
<p>先将使用到对应静态库的源文件编译为.o文件，再进行链接</p>
<div class="language-text highlight"><pre><span></span><code><span id="__span-4-1"><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a>gcc(g++) &lt;ofilelist...&gt; -L&lt;dir&gt; -l&lt;lib&gt;
</span><span id="__span-4-2"><a id="__codelineno-4-2" name="__codelineno-4-2" href="#__codelineno-4-2"></a># 示例
</span><span id="__span-4-3"><a id="__codelineno-4-3" name="__codelineno-4-3" href="#__codelineno-4-3"></a>gcc main.o hello.o -L../lib -lfun1 -lfun2 -lfun3
</span><span id="__span-4-4"><a id="__codelineno-4-4" name="__codelineno-4-4" href="#__codelineno-4-4"></a># 或者
</span><span id="__span-4-5"><a id="__codelineno-4-5" name="__codelineno-4-5" href="#__codelineno-4-5"></a>gcc main.o hello.o ../lib/libfun1.dll ../lib/libfun2.dll ../lib/libfun3.dll
</span></code></pre></div>
<h1 id="makefile">Makefile学习环境搭建<a class="headerlink" href="#makefile" title="Permanent link">&para;</a></h1>
<h3 id="linuxubuntu">Linux（以Ubuntu为例）<a class="headerlink" href="#linuxubuntu" title="Permanent link">&para;</a></h3>
<div class="language-shell highlight"><pre><span></span><code><span id="__span-5-1"><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a>sudo<span class="w"> </span>apt<span class="w"> </span>install<span class="w"> </span>gcc<span class="w"> </span>g++<span class="w"> </span>make
</span></code></pre></div>
<h3 id="windows">Windows<a class="headerlink" href="#windows" title="Permanent link">&para;</a></h3>
<p>学习与演示过程以Windows为主，Windows上装MinGW环境，MinGW官网： <a href="https://www.mingw-w64.org/">https://www.mingw-w64.org/</a>
之前我们提过两个版本的环境，<strong>MingW-W64-builds**和**w64devkit</strong>
推荐使用**w64devkit**套件，里面工具比较齐全，还提供模拟了许多Linux命令，用这个套件环境来学习可以保持在Linux与Windows上Makefile书写方式一致。
以下是w64devkit与其他包一些命令的区别</p>
<table>
<thead>
<tr>
<th align="center">w64devkit（模拟Linux）</th>
<th align="center">MingW-W64-builds或其他套件（Windows cmd命令）</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">make</td>
<td align="center">mingw32-make</td>
</tr>
<tr>
<td align="center">cc</td>
<td align="center">gcc</td>
</tr>
<tr>
<td align="center">rm</td>
<td align="center">del</td>
</tr>
<tr>
<td align="center">touch</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">ls</td>
<td align="center">dir</td>
</tr>
<tr>
<td align="center">sh</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">mv</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">cp</td>
<td align="center">copy/xcopy</td>
</tr>
<tr>
<td align="center">sed</td>
<td align="center"></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h3 id="_9">学习材料<a class="headerlink" href="#_9" title="Permanent link">&para;</a></h3>
<p>make官方文档： <a href="https://www.gnu.org/software/make/manual/make.html">https://www.gnu.org/software/make/manual/make.html</a></p>
<p>2048: <a href="https://github.com/plibither8/2048.cpp">https://github.com/plibither8/2048.cpp</a></p>
<p>sudoku:  <a href="https://github.com/mayerui/sudoku">https://github.com/mayerui/sudoku</a>
&nbsp;
&nbsp;
&nbsp;
&nbsp;</p>
<h1 id="makefile_1">Makefile基础知识<a class="headerlink" href="#makefile_1" title="Permanent link">&para;</a></h1>
<h2 id="make">make使用流程<a class="headerlink" href="#make" title="Permanent link">&para;</a></h2>
<ol>
<li>准备好需要编译的源代码</li>
<li>编写Makefile文件</li>
<li>在命令行执行make命令</li>
</ol>
<p>&nbsp;
&nbsp;</p>
<h2 id="makefile_2">最简单的Makefile<a class="headerlink" href="#makefile_2" title="Permanent link">&para;</a></h2>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-6-1"><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a><span class="nf">hello</span><span class="o">:</span><span class="w"> </span><span class="n">hello</span>.<span class="n">cpp</span>
</span><span id="__span-6-2"><a id="__codelineno-6-2" name="__codelineno-6-2" href="#__codelineno-6-2"></a><span class="w">    </span>g++<span class="w"> </span>hello.cpp<span class="w"> </span>-o<span class="w"> </span>hello<span class="w"> </span><span class="c1"># 开头必须为一个Tab，不能为空格</span>
</span></code></pre></div>
<p><strong>但通常需要将编译与链接分开写，分为如下两步</strong></p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-7-1"><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a><span class="nf">hello</span><span class="o">:</span><span class="w"> </span><span class="n">hello</span>.<span class="n">o</span>
</span><span id="__span-7-2"><a id="__codelineno-7-2" name="__codelineno-7-2" href="#__codelineno-7-2"></a><span class="w">    </span>g++<span class="w"> </span>hello.o<span class="w"> </span>-o<span class="w"> </span>hello
</span><span id="__span-7-3"><a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a><span class="nf">hello.o</span><span class="o">:</span><span class="w"> </span><span class="n">hello</span>.<span class="n">cpp</span>
</span><span id="__span-7-4"><a id="__codelineno-7-4" name="__codelineno-7-4" href="#__codelineno-7-4"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>hello.cpp
</span></code></pre></div>
<p>&nbsp;</p>
<p><strong>规则</strong>(Rules)：一个Makefile文件由一条一条的规则构成，一条规则结构如下</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-8-1"><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="nf">target … (目标)</span><span class="o">:</span><span class="w"> </span><span class="n">prerequisites</span> …(依赖)
</span><span id="__span-8-2"><a id="__codelineno-8-2" name="__codelineno-8-2" href="#__codelineno-8-2"></a><span class="w">        </span>recipe<span class="o">(</span>方法<span class="o">)</span>
</span><span id="__span-8-3"><a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a><span class="w">        </span>…
</span><span id="__span-8-4"><a id="__codelineno-8-4" name="__codelineno-8-4" href="#__codelineno-8-4"></a><span class="w">        </span>…
</span></code></pre></div>
<p>第二种写法</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-9-1"><a id="__codelineno-9-1" name="__codelineno-9-1" href="#__codelineno-9-1"></a><span class="nf">target … (目标)</span><span class="o">:</span><span class="w"> </span><span class="n">prerequisites</span> …(依赖); <span class="n">recipe</span>(方法) ;…
</span></code></pre></div>
<p>&nbsp;
&nbsp;
Make主要用于处理C和C++的编译工作，但不只能处理C和C++，所有编译器/解释器能在命令行终端运行的编程语言都可以处理(例如Java、Python、 Golang....)。Make也不只能用来处理编程语言，所有基于一些文件(依赖)的改变去更新另一些文件(目标)的工作都可以做。</p>
<p><strong>Make编译与打包Java程序示例</strong></p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-10-1"><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="nf">snake.jar </span><span class="o">:</span><span class="w"> </span><span class="n">C</span>.<span class="n">class</span> <span class="n">Main</span>.<span class="n">class</span> <span class="n">SnakeFrame</span>.<span class="n">class</span> <span class="n">SnakePanel</span>.<span class="n">class</span>
</span><span id="__span-10-2"><a id="__codelineno-10-2" name="__codelineno-10-2" href="#__codelineno-10-2"></a><span class="w">    </span>jar<span class="w"> </span>-cvfe<span class="w"> </span>snake.jar<span class="w"> </span>Main<span class="w"> </span>*.class
</span><span id="__span-10-3"><a id="__codelineno-10-3" name="__codelineno-10-3" href="#__codelineno-10-3"></a>
</span><span id="__span-10-4"><a id="__codelineno-10-4" name="__codelineno-10-4" href="#__codelineno-10-4"></a><span class="nf">C.class </span><span class="o">:</span><span class="w"> </span><span class="n">C</span>.<span class="n">java</span>
</span><span id="__span-10-5"><a id="__codelineno-10-5" name="__codelineno-10-5" href="#__codelineno-10-5"></a><span class="w">    </span>javac<span class="w"> </span>C.java
</span><span id="__span-10-6"><a id="__codelineno-10-6" name="__codelineno-10-6" href="#__codelineno-10-6"></a>
</span><span id="__span-10-7"><a id="__codelineno-10-7" name="__codelineno-10-7" href="#__codelineno-10-7"></a><span class="nf">Main.class </span><span class="o">:</span><span class="w"> </span><span class="n">Main</span>.<span class="n">java</span>
</span><span id="__span-10-8"><a id="__codelineno-10-8" name="__codelineno-10-8" href="#__codelineno-10-8"></a><span class="w">    </span>javac<span class="w"> </span>Main.java
</span><span id="__span-10-9"><a id="__codelineno-10-9" name="__codelineno-10-9" href="#__codelineno-10-9"></a>
</span><span id="__span-10-10"><a id="__codelineno-10-10" name="__codelineno-10-10" href="#__codelineno-10-10"></a><span class="nf">SnakeFrame.class </span><span class="o">:</span><span class="w"> </span><span class="n">SnakeFrame</span>.<span class="n">java</span>
</span><span id="__span-10-11"><a id="__codelineno-10-11" name="__codelineno-10-11" href="#__codelineno-10-11"></a><span class="w">    </span>javac<span class="w"> </span>SnakeFrame.java
</span><span id="__span-10-12"><a id="__codelineno-10-12" name="__codelineno-10-12" href="#__codelineno-10-12"></a>
</span><span id="__span-10-13"><a id="__codelineno-10-13" name="__codelineno-10-13" href="#__codelineno-10-13"></a><span class="nf">SnakePanel.class </span><span class="o">:</span><span class="w"> </span><span class="n">SnakePanel</span>.<span class="n">java</span>
</span><span id="__span-10-14"><a id="__codelineno-10-14" name="__codelineno-10-14" href="#__codelineno-10-14"></a><span class="w">    </span>javac<span class="w"> </span>SnakePanel.java
</span><span id="__span-10-15"><a id="__codelineno-10-15" name="__codelineno-10-15" href="#__codelineno-10-15"></a>
</span><span id="__span-10-16"><a id="__codelineno-10-16" name="__codelineno-10-16" href="#__codelineno-10-16"></a><span class="nf">.PHONY</span><span class="o">:</span><span class="w"> </span><span class="n">clean</span>
</span><span id="__span-10-17"><a id="__codelineno-10-17" name="__codelineno-10-17" href="#__codelineno-10-17"></a><span class="nf">clean</span><span class="o">:</span>
</span><span id="__span-10-18"><a id="__codelineno-10-18" name="__codelineno-10-18" href="#__codelineno-10-18"></a><span class="w">    </span>rm<span class="w"> </span>*.class<span class="w"> </span>*.jar
</span></code></pre></div>
<p>&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;</p>
<h2 id="makefile_3">Makefile文件的命名与指定<a class="headerlink" href="#makefile_3" title="Permanent link">&para;</a></h2>
<p>Make会自动查找makefile文件，查找顺序为GNUmakefile -&gt; makefile -&gt; Makefile</p>
<p><strong>GNUmakefile</strong>：不建议使用，因为只有GNU make会识别，其他版本的make（如BSD make, Windows nmake等）不会识别，如果只给GNU make使用的情况</p>
<p><strong>makefile</strong>：可以使用，GNU make和其他版本make识别</p>
<p><strong>Makefile</strong>：最常用，强烈建议使用</p>
<p>如果运行make的时候没有找到以上名字的文件，则会报错，这时候可以手动指定文件名</p>
<div class="language-shell highlight"><pre><span></span><code><span id="__span-11-1"><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a>make<span class="w"> </span>-f<span class="w"> </span>mkfile<span class="w">  </span><span class="c1"># make -f &lt;filename&gt;</span>
</span><span id="__span-11-2"><a id="__codelineno-11-2" name="__codelineno-11-2" href="#__codelineno-11-2"></a>make<span class="w"> </span>--file<span class="o">=</span>mkfile<span class="w"> </span><span class="c1"># make --file=&lt;filename&gt;</span>
</span></code></pre></div>
<blockquote>
<p>手动指定之后，make就会使用指定的文件，即使有Makefile或者makefile不会再自动使用</p>
</blockquote>
<p>&nbsp;
&nbsp;
&nbsp;</p>
<h2 id="makefile_4">Makefile文件内容组成<a class="headerlink" href="#makefile_4" title="Permanent link">&para;</a></h2>
<p>一个Makefile文件通常由五种类型的内容组成：显式规则、隐式规则、变量定义、指令和注释</p>
<p><strong>显式规则</strong>(<em>explicit rules</em>)：显式指明何时以及如何生成或更新目标文件，显式规则包括目标、依赖和更新方法三个部分</p>
<p><strong>隐式规则</strong>(<em>implicit rules</em>)：根据文件自动推导如何从依赖生成或更新目标文件。</p>
<p><strong>变量定义</strong>(<em>variable definitions</em>)：定议变量并指定值，值都是字符串，类似C语言中的宏定义(#define)，在使用时将值展开到引用位置</p>
<p><strong>指令</strong>(<em>directives</em>)：在make读取Makefile的过程中做一些特别的操作，包括：</p>
<ol>
<li>
<p>读取(包含)另一个makefile文件(类似C语言中的#include)</p>
</li>
<li>
<p>确定是否使用或略过makefile文件中的一部分内容(类似C语言中的#if)</p>
</li>
<li>
<p>定义多行变量</p>
</li>
</ol>
<p><strong>注释</strong>(<em>comments</em>)：一行当中 # 后面的内容都是注释，不会被make执行。make当中只有单行注释。如果需要用到#而不是注释，用\#。</p>
<p>&nbsp;
&nbsp;
&nbsp;
&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2 id="makefile_5">一个稍微复杂的Makefile<a class="headerlink" href="#makefile_5" title="Permanent link">&para;</a></h2>
<p><img alt="" src="https://pic.tim-wcx.ltd/img/1.png" /></p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-12-1"><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="nf">sudoku</span><span class="o">:</span><span class="w"> </span><span class="n">block</span>.<span class="n">o</span> <span class="n">command</span>.<span class="n">o</span> <span class="n">input</span>.<span class="n">o</span> <span class="n">main</span>.<span class="n">o</span> <span class="n">scene</span>.<span class="n">o</span> <span class="n">test</span>.<span class="n">o</span>
</span><span id="__span-12-2"><a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="w">    </span>g++<span class="w"> </span>-o<span class="w"> </span>sudoku<span class="w"> </span>block.o<span class="w"> </span>command.o<span class="w"> </span>input.o<span class="w"> </span>main.o<span class="w"> </span>scene.o<span class="w"> </span>test.o
</span><span id="__span-12-3"><a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a>
</span><span id="__span-12-4"><a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="nf">block.o</span><span class="o">:</span><span class="w"> </span><span class="n">block</span>.<span class="n">cpp</span> <span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">color</span>.<span class="n">h</span>
</span><span id="__span-12-5"><a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>block.cpp
</span><span id="__span-12-6"><a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a>
</span><span id="__span-12-7"><a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="nf">command.o</span><span class="o">:</span><span class="w"> </span><span class="n">command</span>.<span class="n">cpp</span> <span class="n">scene</span>.<span class="n">h</span> <span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">command</span>.<span class="n">h</span>
</span><span id="__span-12-8"><a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>command.cpp
</span><span id="__span-12-9"><a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a>
</span><span id="__span-12-10"><a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a><span class="nf">input.o</span><span class="o">:</span><span class="w"> </span><span class="n">input</span>.<span class="n">cpp</span> <span class="n">common</span>.<span class="n">h</span> <span class="n">utility</span>.<span class="n">inl</span>
</span><span id="__span-12-11"><a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>input.cpp
</span><span id="__span-12-12"><a id="__codelineno-12-12" name="__codelineno-12-12" href="#__codelineno-12-12"></a>
</span><span id="__span-12-13"><a id="__codelineno-12-13" name="__codelineno-12-13" href="#__codelineno-12-13"></a><span class="nf">main.o</span><span class="o">:</span><span class="w"> </span><span class="n">main</span>.<span class="n">cpp</span> <span class="n">scene</span>.<span class="n">h</span> <span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">command</span>.<span class="n">h</span> <span class="n">input</span>.<span class="n">h</span>
</span><span id="__span-12-14"><a id="__codelineno-12-14" name="__codelineno-12-14" href="#__codelineno-12-14"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>main.cpp
</span><span id="__span-12-15"><a id="__codelineno-12-15" name="__codelineno-12-15" href="#__codelineno-12-15"></a>
</span><span id="__span-12-16"><a id="__codelineno-12-16" name="__codelineno-12-16" href="#__codelineno-12-16"></a><span class="nf">scene.o</span><span class="o">:</span><span class="w"> </span><span class="n">scene</span>.<span class="n">cpp</span> <span class="n">common</span>.<span class="n">h</span> <span class="n">scene</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">command</span>.<span class="n">h</span> <span class="n">utility</span>.<span class="n">inl</span>
</span><span id="__span-12-17"><a id="__codelineno-12-17" name="__codelineno-12-17" href="#__codelineno-12-17"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>scene.cpp
</span><span id="__span-12-18"><a id="__codelineno-12-18" name="__codelineno-12-18" href="#__codelineno-12-18"></a>
</span><span id="__span-12-19"><a id="__codelineno-12-19" name="__codelineno-12-19" href="#__codelineno-12-19"></a><span class="nf">test.o</span><span class="o">:</span><span class="w"> </span><span class="n">test</span>.<span class="n">cpp</span> <span class="n">test</span>.<span class="n">h</span> <span class="n">scene</span>.<span class="n">h</span> <span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">command</span>.<span class="n">h</span>
</span><span id="__span-12-20"><a id="__codelineno-12-20" name="__codelineno-12-20" href="#__codelineno-12-20"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>test.cpp
</span><span id="__span-12-21"><a id="__codelineno-12-21" name="__codelineno-12-21" href="#__codelineno-12-21"></a>
</span><span id="__span-12-22"><a id="__codelineno-12-22" name="__codelineno-12-22" href="#__codelineno-12-22"></a><span class="nf">hello.o</span><span class="o">:</span><span class="w"> </span><span class="n">hello</span>.<span class="n">cpp</span>
</span><span id="__span-12-23"><a id="__codelineno-12-23" name="__codelineno-12-23" href="#__codelineno-12-23"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>hello.cpp
</span><span id="__span-12-24"><a id="__codelineno-12-24" name="__codelineno-12-24" href="#__codelineno-12-24"></a>
</span><span id="__span-12-25"><a id="__codelineno-12-25" name="__codelineno-12-25" href="#__codelineno-12-25"></a>
</span><span id="__span-12-26"><a id="__codelineno-12-26" name="__codelineno-12-26" href="#__codelineno-12-26"></a><span class="nf">clean</span><span class="o">:</span>
</span><span id="__span-12-27"><a id="__codelineno-12-27" name="__codelineno-12-27" href="#__codelineno-12-27"></a><span class="w">    </span>rm<span class="w"> </span>block.o<span class="w"> </span>command.o<span class="w"> </span>input.o<span class="w"> </span>main.o<span class="w"> </span>scene.o<span class="w"> </span>test.o
</span><span id="__span-12-28"><a id="__codelineno-12-28" name="__codelineno-12-28" href="#__codelineno-12-28"></a><span class="w">    </span>rm<span class="w"> </span>sudoku.exe
</span></code></pre></div>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-13-1"><a id="__codelineno-13-1" name="__codelineno-13-1" href="#__codelineno-13-1"></a><span class="nf">target … (目标)</span><span class="o">:</span><span class="w"> </span><span class="n">prerequisites</span> …(依赖)
</span><span id="__span-13-2"><a id="__codelineno-13-2" name="__codelineno-13-2" href="#__codelineno-13-2"></a><span class="w">        </span>recipe<span class="o">(</span>方法<span class="o">)</span>
</span><span id="__span-13-3"><a id="__codelineno-13-3" name="__codelineno-13-3" href="#__codelineno-13-3"></a><span class="w">        </span>…
</span><span id="__span-13-4"><a id="__codelineno-13-4" name="__codelineno-13-4" href="#__codelineno-13-4"></a><span class="w">        </span>…
</span></code></pre></div>
<h2 id="_10">目标<a class="headerlink" href="#_10" title="Permanent link">&para;</a></h2>
<ol>
<li>
<p>Makefile中会有很多目标，但最终目标只有一个，其他所有内容都是为这个最终目标服务的，写Makefile的时候**先写出最终目标，再依次解决总目标的依赖**</p>
</li>
<li>
<p>一般情况第一条规则中的目标会被确立为最终目标，第一条规则默认会被make执行</p>
</li>
<li>
<p>通常来说目标是一个文件，一条规则的目的就是生成或更新目标文件。</p>
</li>
<li>
<p>make会根据目标文件和依赖文件最后修改时间判断是否需要执行更新目标文件的方法。如果目标文件不存在或者目标文件最后修改时间早于其中一个依赖文件最后修改时间，则重新执行更新目标文件的方法。否则不会执行。</p>
</li>
<li>
<p>除了最终目标对应的更新方法默认会执行外，如果Makefile中一个目标不是其他目标的依赖，那么这个目标对应的规则不会自动执行。需要手动指定，方法为</p>
</li>
</ol>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-14-1"><a id="__codelineno-14-1" name="__codelineno-14-1" href="#__codelineno-14-1"></a><span class="err">make</span><span class="w"> </span><span class="err">&lt;target&gt;</span><span class="w">  </span><span class="c"># 如 make clean , make hello.o</span>
</span></code></pre></div>
<ol>
<li>可以使用.DEFAULT_GOAL来修改默认最终目标</li>
</ol>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-15-1"><a id="__codelineno-15-1" name="__codelineno-15-1" href="#__codelineno-15-1"></a><span class="nv">.DEFAULT_GOAL</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>main
</span><span id="__span-15-2"><a id="__codelineno-15-2" name="__codelineno-15-2" href="#__codelineno-15-2"></a>
</span><span id="__span-15-3"><a id="__codelineno-15-3" name="__codelineno-15-3" href="#__codelineno-15-3"></a><span class="nf">all</span><span class="o">:</span><span class="w"> </span>
</span><span id="__span-15-4"><a id="__codelineno-15-4" name="__codelineno-15-4" href="#__codelineno-15-4"></a><span class="w">    </span>@echo<span class="w"> </span>all
</span><span id="__span-15-5"><a id="__codelineno-15-5" name="__codelineno-15-5" href="#__codelineno-15-5"></a>
</span><span id="__span-15-6"><a id="__codelineno-15-6" name="__codelineno-15-6" href="#__codelineno-15-6"></a><span class="nf">main</span><span class="o">:</span>
</span><span id="__span-15-7"><a id="__codelineno-15-7" name="__codelineno-15-7" href="#__codelineno-15-7"></a><span class="w">    </span>@echo<span class="w"> </span>main
</span></code></pre></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2 id="_11">伪目标<a class="headerlink" href="#_11" title="Permanent link">&para;</a></h2>
<p>如果一个标并不是一个文件，则这个目标就是伪目标。例如前面的clean目标。如果说在当前目录下有一个文件名称和这个目标名称冲突了，则这个目标就没法执行。这时候需要用到一个特殊的目标 .PHONY，将上面的clean目标改写如下 </p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-16-1"><a id="__codelineno-16-1" name="__codelineno-16-1" href="#__codelineno-16-1"></a><span class="nf">.PHONY</span><span class="o">:</span><span class="w"> </span><span class="n">clean</span>
</span><span id="__span-16-2"><a id="__codelineno-16-2" name="__codelineno-16-2" href="#__codelineno-16-2"></a><span class="nf">clean</span><span class="o">:</span>
</span><span id="__span-16-3"><a id="__codelineno-16-3" name="__codelineno-16-3" href="#__codelineno-16-3"></a><span class="w">    </span>rm<span class="w"> </span>block.o<span class="w"> </span>command.o<span class="w"> </span>input.o<span class="w"> </span>main.o<span class="w"> </span>scene.o<span class="w"> </span>test.o
</span><span id="__span-16-4"><a id="__codelineno-16-4" name="__codelineno-16-4" href="#__codelineno-16-4"></a><span class="w">    </span>rm<span class="w"> </span>sudoku.exe
</span></code></pre></div>
<p>这样即使当前目录下存在与目标同名的文件，该目标也能正常执行。</p>
<p><strong>伪目标的其他应用方式</strong></p>
<p>如果一条规则的依赖文件没有改动，则不会执行对应的更新方法。如果需要每次不论有没有改动都执行某一目标的更新方法，可以把对应的目标添加到.PHONY的依赖中，例如下面这种方式，则每次执行make都会更新test.o，不管其依赖文件有没有改动</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-17-1"><a id="__codelineno-17-1" name="__codelineno-17-1" href="#__codelineno-17-1"></a><span class="nf">test.o</span><span class="o">:</span><span class="w"> </span><span class="n">test</span>.<span class="n">cpp</span> <span class="n">test</span>.<span class="n">h</span>
</span><span id="__span-17-2"><a id="__codelineno-17-2" name="__codelineno-17-2" href="#__codelineno-17-2"></a><span class="w">        </span>g++<span class="w"> </span>-c<span class="w"> </span>test.cpp
</span><span id="__span-17-3"><a id="__codelineno-17-3" name="__codelineno-17-3" href="#__codelineno-17-3"></a>
</span><span id="__span-17-4"><a id="__codelineno-17-4" name="__codelineno-17-4" href="#__codelineno-17-4"></a><span class="nf">.PHONY</span><span class="o">:</span><span class="w"> </span><span class="n">clean</span> <span class="n">test</span>.<span class="n">o</span>
</span></code></pre></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2 id="_12">依赖<a class="headerlink" href="#_12" title="Permanent link">&para;</a></h2>
<h3 id="_13">依赖类型<a class="headerlink" href="#_13" title="Permanent link">&para;</a></h3>
<p><strong>普通依赖</strong></p>
<p>前面说过的这种形式都是普通依赖。直接列在目标后面。普通依赖有两个特点：</p>
<ol>
<li>如果这一依赖是由其他规则生成的文件，那么执行到这一目标前会先执行生成依赖的那一规则 </li>
<li>如果任何一个依赖文件修改时间比目标晚，那么就重新生成目标文件</li>
</ol>
<p><strong>order-only依赖</strong></p>
<p>依赖文件不存在时，会执行对应的方法生成，但依赖文件更新并不会导致目标文件的更新</p>
<p>如果目标文件已存在，order-only依赖中的文件即使修改时间比目标文件晚，目标文件也不会更新。</p>
<p>定义方法如下：</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-18-1"><a id="__codelineno-18-1" name="__codelineno-18-1" href="#__codelineno-18-1"></a><span class="nf">targets </span><span class="o">:</span><span class="w"> </span><span class="n">normal</span>-<span class="n">prerequisites</span> <span class="p">|</span> <span class="n">order</span>-<span class="n">only</span>-<span class="n">prerequisites</span>
</span></code></pre></div>
<p>normal-prerequisites部分可以为空</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3 id="_14">指定依赖搜索路径<a class="headerlink" href="#_14" title="Permanent link">&para;</a></h3>
<p>make默认在Makefile文件所在的目录下查找依赖文件，如果找不到，就会报错。这时候就需要手动指定搜索路径，用VPATH变量或vpath指令。</p>
<p><strong>VPATH用法如下：</strong></p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-19-1"><a id="__codelineno-19-1" name="__codelineno-19-1" href="#__codelineno-19-1"></a><span class="nv">VPATH</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>&lt;dir1&gt;:&lt;dir2&gt;:&lt;dir3&gt;...
</span><span id="__span-19-2"><a id="__codelineno-19-2" name="__codelineno-19-2" href="#__codelineno-19-2"></a><span class="c"># 例如</span>
</span><span id="__span-19-3"><a id="__codelineno-19-3" name="__codelineno-19-3" href="#__codelineno-19-3"></a><span class="nv">VPATH</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>include:src
</span></code></pre></div>
<p>多个目录之间冒号隔开，这时make会在VPATH指定的这些目录里面查找依赖文件。</p>
<p><strong>vpath指令用法：</strong></p>
<p>vpath比VPATH使用更灵活，可以指定某个类型的文件在哪个目录搜索。</p>
<p>用法如下：</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-20-1"><a id="__codelineno-20-1" name="__codelineno-20-1" href="#__codelineno-20-1"></a><span class="cp">vpath &lt;pattern&gt; &lt;directories&gt;</span>
</span><span id="__span-20-2"><a id="__codelineno-20-2" name="__codelineno-20-2" href="#__codelineno-20-2"></a>
</span><span id="__span-20-3"><a id="__codelineno-20-3" name="__codelineno-20-3" href="#__codelineno-20-3"></a><span class="cp">vpath %.h include  # .h文件在include目录下查找</span>
</span><span id="__span-20-4"><a id="__codelineno-20-4" name="__codelineno-20-4" href="#__codelineno-20-4"></a><span class="cp">vpath %.h include:headers  # .h文件在include或headers文件下查找</span>
</span><span id="__span-20-5"><a id="__codelineno-20-5" name="__codelineno-20-5" href="#__codelineno-20-5"></a>
</span><span id="__span-20-6"><a id="__codelineno-20-6" name="__codelineno-20-6" href="#__codelineno-20-6"></a><span class="cp">vpath % src   # 所有文件都在src下查找</span>
</span><span id="__span-20-7"><a id="__codelineno-20-7" name="__codelineno-20-7" href="#__codelineno-20-7"></a>
</span><span id="__span-20-8"><a id="__codelineno-20-8" name="__codelineno-20-8" href="#__codelineno-20-8"></a><span class="cp">vpath hello.cpp src  # hello.cpp文件在src查找</span>
</span></code></pre></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2 id="_15">更新方法<a class="headerlink" href="#_15" title="Permanent link">&para;</a></h2>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-21-1"><a id="__codelineno-21-1" name="__codelineno-21-1" href="#__codelineno-21-1"></a><span class="nf">target … (目标)</span><span class="o">:</span><span class="w"> </span><span class="n">prerequisites</span> …(依赖)
</span><span id="__span-21-2"><a id="__codelineno-21-2" name="__codelineno-21-2" href="#__codelineno-21-2"></a><span class="w">        </span>recipe<span class="o">(</span>方法<span class="o">)</span>
</span><span id="__span-21-3"><a id="__codelineno-21-3" name="__codelineno-21-3" href="#__codelineno-21-3"></a><span class="w">        </span>…
</span><span id="__span-21-4"><a id="__codelineno-21-4" name="__codelineno-21-4" href="#__codelineno-21-4"></a><span class="w">        </span>…
</span></code></pre></div>
<h4 id="_16">关于执行终端<a class="headerlink" href="#_16" title="Permanent link">&para;</a></h4>
<p>更新方法实际上是一些Shell指令，通常以Tab开头，或直接放在目标-依赖列表后面，用分号隔开。这些指令都需要交给Shell执行，所以需要符合Shell语法。默认使用的Shell是sh，在Windows上如果没有安装sh.exe的话会自动查找使用cmd.exe之类的终端。这时有的指令写法，例如循环语句，与Linux不同，需要注意。</p>
<p>可以通过SHELL变量手动指定Shell</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-22-1"><a id="__codelineno-22-1" name="__codelineno-22-1" href="#__codelineno-22-1"></a><span class="nv">SHELL</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe
</span><span id="__span-22-2"><a id="__codelineno-22-2" name="__codelineno-22-2" href="#__codelineno-22-2"></a><span class="nv">SHELL</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>cmd.exe
</span></code></pre></div>
<p>默认的执行方式为一条指令重新调用一个Shell进程来执行。有时为了提高性能或其他原因，想让这个目标的所有指令都在同一进程中执行，可以在Makefile中添加 .ONESHELL</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-23-1"><a id="__codelineno-23-1" name="__codelineno-23-1" href="#__codelineno-23-1"></a><span class="w"> </span>.ONESHELL:
</span></code></pre></div>
<p>这样所有指令都会在同一次Shell调用中执行</p>
<h4 id="shell">Shell语句回显问题<a class="headerlink" href="#shell" title="Permanent link">&para;</a></h4>
<p>通常make在执行一条Shell语句前都会先打印这条语句，如果不想打印可以在语句开头在@</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-24-1"><a id="__codelineno-24-1" name="__codelineno-24-1" href="#__codelineno-24-1"></a><span class="err">@echo</span><span class="w"> </span><span class="err">hello</span>
</span><span id="__span-24-2"><a id="__codelineno-24-2" name="__codelineno-24-2" href="#__codelineno-24-2"></a><span class="err">@g++</span><span class="w"> </span><span class="err">-o</span><span class="w"> </span><span class="err">hello</span><span class="w"> </span><span class="err">hello.cpp</span>
</span></code></pre></div>
<p>也可以使用.SILENT来指定哪些目标的更新方法指令不用打印</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-25-1"><a id="__codelineno-25-1" name="__codelineno-25-1" href="#__codelineno-25-1"></a><span class="nf">.SILENT</span><span class="o">:</span><span class="w"> </span><span class="n">main</span> <span class="n">all</span>
</span></code></pre></div>
<h4 id="_17">错误处理<a class="headerlink" href="#_17" title="Permanent link">&para;</a></h4>
<p>如果一条规则当中包含多条Shell指令，每条指令执行完之后make都会检查返回状态，如果返回状态是0，则执行成功，继续执行下一条指令，直到最后一条指令执行完成之后，一条规则也就结束了。</p>
<p>如果过程中发生了错误，即某一条指令的返回值不是0，那么make就会终止执行当前规则中剩下的Shell指令。</p>
<p>例如</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-26-1"><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="nf">clean</span><span class="o">:</span>
</span><span id="__span-26-2"><a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a><span class="w">    </span>rm<span class="w"> </span>main.o<span class="w"> </span>hello.o
</span><span id="__span-26-3"><a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="w">    </span>rm<span class="w"> </span>main.exe
</span></code></pre></div>
<p>这时如果第一条rm main.o hello.o出错，第二条rm main.exe就不会执行。类似情况下，希望make忽视错误继续下一条指令。在指令开头<code>-</code>可以达到这种效果。</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-27-1"><a id="__codelineno-27-1" name="__codelineno-27-1" href="#__codelineno-27-1"></a><span class="nf">clean</span><span class="o">:</span>
</span><span id="__span-27-2"><a id="__codelineno-27-2" name="__codelineno-27-2" href="#__codelineno-27-2"></a><span class="w">    </span>-rm<span class="w"> </span>main.o<span class="w"> </span>hello.o
</span><span id="__span-27-3"><a id="__codelineno-27-3" name="__codelineno-27-3" href="#__codelineno-27-3"></a><span class="w">    </span>-rm<span class="w"> </span>main.exe
</span></code></pre></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2 id="_18">变量应用<a class="headerlink" href="#_18" title="Permanent link">&para;</a></h2>
<p>Makefile中的变量有点类似C语言中的宏定义，即用一个名称表示一串文本。但与C语言宏定义不同的是，Makefile的变量值是可以改变的。变量定义之后可以在目标、依赖、方法等Makefile文件的任意地方进行引用。</p>
<blockquote>
<p>Makefile中的变量值只有一种类型： 字符串</p>
</blockquote>
<p><strong>变量可以用来表示什么</strong></p>
<ul>
<li>
<p>文件名序列</p>
</li>
<li>
<p>编译选项</p>
</li>
<li>
<p>需要运行的程序</p>
</li>
<li>
<p>需要进行操作的路径</p>
</li>
<li>
<p>......</p>
</li>
</ul>
<h3 id="_19">变量定义与引用方式<a class="headerlink" href="#_19" title="Permanent link">&para;</a></h3>
<p><strong>定义方式</strong></p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-28-1"><a id="__codelineno-28-1" name="__codelineno-28-1" href="#__codelineno-28-1"></a><span class="c"># &lt;变量名&gt; = &lt;变量值&gt;  &lt;变量名&gt; := &lt;变量值&gt;  &lt;变量名&gt; ::= &lt;变量值&gt;</span>
</span><span id="__span-28-2"><a id="__codelineno-28-2" name="__codelineno-28-2" href="#__codelineno-28-2"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>main.cpp<span class="w"> </span>hello.cpp
</span><span id="__span-28-3"><a id="__codelineno-28-3" name="__codelineno-28-3" href="#__codelineno-28-3"></a><span class="nv">objects</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span>main.o<span class="w"> </span>hello.o
</span><span id="__span-28-4"><a id="__codelineno-28-4" name="__codelineno-28-4" href="#__codelineno-28-4"></a><span class="nf">var3 </span><span class="o">::</span>= <span class="n">main</span>.<span class="n">o</span>
</span></code></pre></div>
<blockquote>
<p>变量名区分大小写，可以是任意字符串，不能含有":", "#", "="</p>
</blockquote>
<p><strong>使用方式</strong></p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-29-1"><a id="__codelineno-29-1" name="__codelineno-29-1" href="#__codelineno-29-1"></a><span class="c"># $(&lt;变量名&gt;) 或者 ${&lt;变量名&gt;}</span>
</span><span id="__span-29-2"><a id="__codelineno-29-2" name="__codelineno-29-2" href="#__codelineno-29-2"></a><span class="nf">main.o </span><span class="o">:</span><span class="w"> </span><span class="k">$(</span><span class="nv">files</span><span class="k">)</span> <span class="c"># 或者 ${files}</span>
</span><span id="__span-29-3"><a id="__codelineno-29-3" name="__codelineno-29-3" href="#__codelineno-29-3"></a><span class="w">    </span>...
</span></code></pre></div>
<blockquote>
<p>如果变量名只有一个字符，使用时可以不用括号，如$a, $b， 但不建议这样用，不管是否只有一个字符都写成$(a), $(b)这种形式</p>
</blockquote>
<h3 id="makefile_6">Makefile读取过程<a class="headerlink" href="#makefile_6" title="Permanent link">&para;</a></h3>
<p>GNU make分两个阶段来执行Makefile，第一阶段(读取阶段)：</p>
<ul>
<li>
<p>读取Makefile文件的所有内容</p>
</li>
<li>
<p>根据Makefile的内容在程序内建立起变量</p>
</li>
<li>
<p>在程序内构建起显式规则、隐式规则</p>
</li>
<li>
<p>建立目标和依赖之间的依赖图</p>
</li>
</ul>
<p>第二阶段(目标更新阶段)：</p>
<ul>
<li>用第一阶段构建起来的数据确定哪个目标需要更新然后执行对应的更新方法</li>
</ul>
<p>变量和函数的展开如果发生在第一阶段，就称作**立即展开**，否则称为**延迟展开**。立即展开的变量或函数在第一个阶段，也就是Makefile被读取解析的时候就进行展开。延迟展开的变量或函数将会到用到的时候才会进行展开，有以下两种情况：</p>
<ul>
<li>
<p>在一个立即展开的表达式中用到</p>
</li>
<li>
<p>在第二个阶段中用到</p>
</li>
</ul>
<p><strong>显式规则中，目标和依赖部分都是立即展开，在更新方法中延迟展开</strong></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3 id="_20">变量赋值<a class="headerlink" href="#_20" title="Permanent link">&para;</a></h3>
<h4 id="_21">递归展开赋值（延迟展开）<a class="headerlink" href="#_21" title="Permanent link">&para;</a></h4>
<p>第一种方式就是直接使用<kbd>=</kbd>，这种方式如果赋值的时候右边是其他变量引用或者函数调用之类的，将不会做处理，直接保留原样，在使用到该变量的时候再来进行处理得到变量值（Makefile执行的第二个阶段再进行变量展开得到变量值）</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-30-1"><a id="__codelineno-30-1" name="__codelineno-30-1" href="#__codelineno-30-1"></a><span class="nv">bar2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>ThisIsBar2No.1
</span><span id="__span-30-2"><a id="__codelineno-30-2" name="__codelineno-30-2" href="#__codelineno-30-2"></a><span class="nv">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>bar<span class="k">)</span>
</span><span id="__span-30-3"><a id="__codelineno-30-3" name="__codelineno-30-3" href="#__codelineno-30-3"></a><span class="nv">foo2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>bar2<span class="k">)</span>
</span><span id="__span-30-4"><a id="__codelineno-30-4" name="__codelineno-30-4" href="#__codelineno-30-4"></a>
</span><span id="__span-30-5"><a id="__codelineno-30-5" name="__codelineno-30-5" href="#__codelineno-30-5"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-30-6"><a id="__codelineno-30-6" name="__codelineno-30-6" href="#__codelineno-30-6"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>foo<span class="k">)</span><span class="w">  </span><span class="c1"># Huh?</span>
</span><span id="__span-30-7"><a id="__codelineno-30-7" name="__codelineno-30-7" href="#__codelineno-30-7"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>foo2<span class="k">)</span><span class="w">  </span><span class="c1"># ThisIsBar2No.2</span>
</span><span id="__span-30-8"><a id="__codelineno-30-8" name="__codelineno-30-8" href="#__codelineno-30-8"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>ugh<span class="k">)</span><span class="w">   </span><span class="c1"># Huh?</span>
</span><span id="__span-30-9"><a id="__codelineno-30-9" name="__codelineno-30-9" href="#__codelineno-30-9"></a>
</span><span id="__span-30-10"><a id="__codelineno-30-10" name="__codelineno-30-10" href="#__codelineno-30-10"></a><span class="nv">bar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>ugh<span class="k">)</span>
</span><span id="__span-30-11"><a id="__codelineno-30-11" name="__codelineno-30-11" href="#__codelineno-30-11"></a><span class="nv">ugh</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Huh?
</span><span id="__span-30-12"><a id="__codelineno-30-12" name="__codelineno-30-12" href="#__codelineno-30-12"></a><span class="nv">bar2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>ThisIsBar2No.2
</span></code></pre></div>
<h4 id="_22">简单赋值(立即展开)<a class="headerlink" href="#_22" title="Permanent link">&para;</a></h4>
<p>简单赋值使用<kbd>:=</kbd>或<kbd>::=</kbd>，这种方式如果等号右边是其他变量或者引用的话，将会在赋值的时候就进行处理得到变量值。（Makefile执行第一阶段进行变量展开）</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-31-1"><a id="__codelineno-31-1" name="__codelineno-31-1" href="#__codelineno-31-1"></a><span class="nv">bar2</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span>ThisIsBar2No.1
</span><span id="__span-31-2"><a id="__codelineno-31-2" name="__codelineno-31-2" href="#__codelineno-31-2"></a><span class="nv">foo</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$(</span>bar<span class="k">)</span>
</span><span id="__span-31-3"><a id="__codelineno-31-3" name="__codelineno-31-3" href="#__codelineno-31-3"></a><span class="nv">foo2</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$(</span>bar2<span class="k">)</span>
</span><span id="__span-31-4"><a id="__codelineno-31-4" name="__codelineno-31-4" href="#__codelineno-31-4"></a>
</span><span id="__span-31-5"><a id="__codelineno-31-5" name="__codelineno-31-5" href="#__codelineno-31-5"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-31-6"><a id="__codelineno-31-6" name="__codelineno-31-6" href="#__codelineno-31-6"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>foo<span class="k">)</span><span class="w">    </span><span class="c1"># 空串，没有内容</span>
</span><span id="__span-31-7"><a id="__codelineno-31-7" name="__codelineno-31-7" href="#__codelineno-31-7"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>foo2<span class="k">)</span><span class="w">    </span><span class="c1"># ThisIsBar2No.1</span>
</span><span id="__span-31-8"><a id="__codelineno-31-8" name="__codelineno-31-8" href="#__codelineno-31-8"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>ugh<span class="k">)</span><span class="w">    </span><span class="c1"># </span>
</span><span id="__span-31-9"><a id="__codelineno-31-9" name="__codelineno-31-9" href="#__codelineno-31-9"></a>
</span><span id="__span-31-10"><a id="__codelineno-31-10" name="__codelineno-31-10" href="#__codelineno-31-10"></a><span class="nv">bar</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$(</span>ugh<span class="k">)</span>
</span><span id="__span-31-11"><a id="__codelineno-31-11" name="__codelineno-31-11" href="#__codelineno-31-11"></a><span class="nv">ugh</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span>Huh?
</span><span id="__span-31-12"><a id="__codelineno-31-12" name="__codelineno-31-12" href="#__codelineno-31-12"></a><span class="nv">bar2</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span>ThisIsBar2No.2
</span></code></pre></div>
<h4 id="_23">条件赋值<a class="headerlink" href="#_23" title="Permanent link">&para;</a></h4>
<p>条件赋值使用<kbd>?=</kbd>，如果变量已经定义过了（即已经有值了），那么就保持原来的值，如果变量还没赋值过，就把右边的值赋给变量。</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-32-1"><a id="__codelineno-32-1" name="__codelineno-32-1" href="#__codelineno-32-1"></a><span class="nv">var1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">100</span>
</span><span id="__span-32-2"><a id="__codelineno-32-2" name="__codelineno-32-2" href="#__codelineno-32-2"></a><span class="nv">var1</span><span class="w"> </span><span class="o">?=</span><span class="w"> </span><span class="m">200</span>
</span><span id="__span-32-3"><a id="__codelineno-32-3" name="__codelineno-32-3" href="#__codelineno-32-3"></a>
</span><span id="__span-32-4"><a id="__codelineno-32-4" name="__codelineno-32-4" href="#__codelineno-32-4"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-32-5"><a id="__codelineno-32-5" name="__codelineno-32-5" href="#__codelineno-32-5"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>var1<span class="k">)</span><span class="w"> </span><span class="c1"># 100 注释var1 = 100之后为200</span>
</span></code></pre></div>
<p><strong>练习</strong>：试求a的值</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-33-1"><a id="__codelineno-33-1" name="__codelineno-33-1" href="#__codelineno-33-1"></a><span class="nv">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello
</span><span id="__span-33-2"><a id="__codelineno-33-2" name="__codelineno-33-2" href="#__codelineno-33-2"></a><span class="nv">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>world
</span><span id="__span-33-3"><a id="__codelineno-33-3" name="__codelineno-33-3" href="#__codelineno-33-3"></a><span class="nv">a</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$(</span>x<span class="k">)$(</span>y<span class="k">)</span>
</span><span id="__span-33-4"><a id="__codelineno-33-4" name="__codelineno-33-4" href="#__codelineno-33-4"></a>
</span><span id="__span-33-5"><a id="__codelineno-33-5" name="__codelineno-33-5" href="#__codelineno-33-5"></a><span class="nv">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>y
</span><span id="__span-33-6"><a id="__codelineno-33-6" name="__codelineno-33-6" href="#__codelineno-33-6"></a><span class="nv">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>z
</span><span id="__span-33-7"><a id="__codelineno-33-7" name="__codelineno-33-7" href="#__codelineno-33-7"></a><span class="nv">a</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$($(</span>x<span class="k">))</span>
</span><span id="__span-33-8"><a id="__codelineno-33-8" name="__codelineno-33-8" href="#__codelineno-33-8"></a>
</span><span id="__span-33-9"><a id="__codelineno-33-9" name="__codelineno-33-9" href="#__codelineno-33-9"></a><span class="nv">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>y
</span><span id="__span-33-10"><a id="__codelineno-33-10" name="__codelineno-33-10" href="#__codelineno-33-10"></a><span class="nv">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>z
</span><span id="__span-33-11"><a id="__codelineno-33-11" name="__codelineno-33-11" href="#__codelineno-33-11"></a><span class="nv">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>u
</span><span id="__span-33-12"><a id="__codelineno-33-12" name="__codelineno-33-12" href="#__codelineno-33-12"></a><span class="nv">a</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$($($(</span>x<span class="k">)))</span>
</span><span id="__span-33-13"><a id="__codelineno-33-13" name="__codelineno-33-13" href="#__codelineno-33-13"></a>
</span><span id="__span-33-14"><a id="__codelineno-33-14" name="__codelineno-33-14" href="#__codelineno-33-14"></a><span class="nv">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>y<span class="k">)</span>
</span><span id="__span-33-15"><a id="__codelineno-33-15" name="__codelineno-33-15" href="#__codelineno-33-15"></a><span class="nv">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>z
</span><span id="__span-33-16"><a id="__codelineno-33-16" name="__codelineno-33-16" href="#__codelineno-33-16"></a><span class="nv">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Hello
</span><span id="__span-33-17"><a id="__codelineno-33-17" name="__codelineno-33-17" href="#__codelineno-33-17"></a><span class="nv">a</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$($(</span>x<span class="k">))</span>
</span></code></pre></div>
<h4 id="_24">追加<a class="headerlink" href="#_24" title="Permanent link">&para;</a></h4>
<p>使用<kbd>+=</kbd>在变量已有的基础上追加内容</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-34-1"><a id="__codelineno-34-1" name="__codelineno-34-1" href="#__codelineno-34-1"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>main.cpp
</span><span id="__span-34-2"><a id="__codelineno-34-2" name="__codelineno-34-2" href="#__codelineno-34-2"></a><span class="nv">files</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span>hello.cpp
</span><span id="__span-34-3"><a id="__codelineno-34-3" name="__codelineno-34-3" href="#__codelineno-34-3"></a>
</span><span id="__span-34-4"><a id="__codelineno-34-4" name="__codelineno-34-4" href="#__codelineno-34-4"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-34-5"><a id="__codelineno-34-5" name="__codelineno-34-5" href="#__codelineno-34-5"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>files<span class="k">)</span>
</span></code></pre></div>
<h4 id="shell_1">Shell运行赋值<a class="headerlink" href="#shell_1" title="Permanent link">&para;</a></h4>
<p>使用<kbd>!=</kbd>，运行一个Shell指令后将返回值赋给一个变量</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-35-1"><a id="__codelineno-35-1" name="__codelineno-35-1" href="#__codelineno-35-1"></a><span class="nv">gcc_version</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span>gcc<span class="w"> </span>--version
</span><span id="__span-35-2"><a id="__codelineno-35-2" name="__codelineno-35-2" href="#__codelineno-35-2"></a><span class="nv">files</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span>ls<span class="w"> </span>.
</span></code></pre></div>
<blockquote>
<p>如果使用Windows需要注意，这种赋值方式只适用于与Linux相同的Shell指令，Windows独有的指令不能这样使用。</p>
</blockquote>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3 id="_25">定义多行变量<a class="headerlink" href="#_25" title="Permanent link">&para;</a></h3>
<p>前面定义的变量都是单行的。</p>
<p>变量值有多行，多用于定义shell指令</p>
<p><strong>语法</strong></p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-36-1"><a id="__codelineno-36-1" name="__codelineno-36-1" href="#__codelineno-36-1"></a><span class="cp">define &lt;varable_name&gt;  # 默认为 = </span>
</span><span id="__span-36-2"><a id="__codelineno-36-2" name="__codelineno-36-2" href="#__codelineno-36-2"></a><span class="c"># 变量内容</span>
</span><span id="__span-36-3"><a id="__codelineno-36-3" name="__codelineno-36-3" href="#__codelineno-36-3"></a><span class="cp">endef</span>
</span><span id="__span-36-4"><a id="__codelineno-36-4" name="__codelineno-36-4" href="#__codelineno-36-4"></a>
</span><span id="__span-36-5"><a id="__codelineno-36-5" name="__codelineno-36-5" href="#__codelineno-36-5"></a><span class="cp">define &lt;varable_name&gt; :=</span>
</span><span id="__span-36-6"><a id="__codelineno-36-6" name="__codelineno-36-6" href="#__codelineno-36-6"></a><span class="c"># 变量内容</span>
</span><span id="__span-36-7"><a id="__codelineno-36-7" name="__codelineno-36-7" href="#__codelineno-36-7"></a><span class="cp">endef</span>
</span><span id="__span-36-8"><a id="__codelineno-36-8" name="__codelineno-36-8" href="#__codelineno-36-8"></a>
</span><span id="__span-36-9"><a id="__codelineno-36-9" name="__codelineno-36-9" href="#__codelineno-36-9"></a><span class="cp">define &lt;varable_name&gt; +=</span>
</span><span id="__span-36-10"><a id="__codelineno-36-10" name="__codelineno-36-10" href="#__codelineno-36-10"></a><span class="c"># 变量内容</span>
</span><span id="__span-36-11"><a id="__codelineno-36-11" name="__codelineno-36-11" href="#__codelineno-36-11"></a><span class="cp">endef</span>
</span></code></pre></div>
<p>&nbsp;</p>
<p><strong>示例</strong></p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-37-1"><a id="__codelineno-37-1" name="__codelineno-37-1" href="#__codelineno-37-1"></a><span class="nv">echosomething</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>@echo<span class="w"> </span>This<span class="w"> </span>is<span class="w"> </span>the<span class="w"> </span>first<span class="w"> </span>line
</span><span id="__span-37-2"><a id="__codelineno-37-2" name="__codelineno-37-2" href="#__codelineno-37-2"></a>
</span><span id="__span-37-3"><a id="__codelineno-37-3" name="__codelineno-37-3" href="#__codelineno-37-3"></a><span class="cp">define echosomething +=  </span>
</span><span id="__span-37-4"><a id="__codelineno-37-4" name="__codelineno-37-4" href="#__codelineno-37-4"></a>
</span><span id="__span-37-5"><a id="__codelineno-37-5" name="__codelineno-37-5" href="#__codelineno-37-5"></a><span class="err">@echo</span><span class="w"> </span><span class="err">hello</span>
</span><span id="__span-37-6"><a id="__codelineno-37-6" name="__codelineno-37-6" href="#__codelineno-37-6"></a><span class="err">@echo</span><span class="w"> </span><span class="err">world</span>
</span><span id="__span-37-7"><a id="__codelineno-37-7" name="__codelineno-37-7" href="#__codelineno-37-7"></a><span class="err">@echo</span><span class="w"> </span><span class="err">3</span>
</span><span id="__span-37-8"><a id="__codelineno-37-8" name="__codelineno-37-8" href="#__codelineno-37-8"></a><span class="cp">endef</span>
</span><span id="__span-37-9"><a id="__codelineno-37-9" name="__codelineno-37-9" href="#__codelineno-37-9"></a>
</span><span id="__span-37-10"><a id="__codelineno-37-10" name="__codelineno-37-10" href="#__codelineno-37-10"></a>
</span><span id="__span-37-11"><a id="__codelineno-37-11" name="__codelineno-37-11" href="#__codelineno-37-11"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-37-12"><a id="__codelineno-37-12" name="__codelineno-37-12" href="#__codelineno-37-12"></a><span class="w">    </span><span class="k">$(</span>echosomething<span class="k">)</span>
</span></code></pre></div>
<h3 id="_26">取消变量<a class="headerlink" href="#_26" title="Permanent link">&para;</a></h3>
<p>如果想清除一个变量，用以下方法</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-38-1"><a id="__codelineno-38-1" name="__codelineno-38-1" href="#__codelineno-38-1"></a><span class="err">undefine</span><span class="w"> </span><span class="err">&lt;变量名&gt;</span><span class="w">   </span><span class="err">如</span><span class="w"> </span><span class="err">undefine</span><span class="w"> </span><span class="err">files,</span><span class="w">  </span><span class="err">undefine</span><span class="w"> </span><span class="err">objs</span>
</span></code></pre></div>
<h3 id="_27">环境变量的使用<a class="headerlink" href="#_27" title="Permanent link">&para;</a></h3>
<p>系统中的环境变量可以直接在Makefile中直接使用，使用方法跟普通变量一样</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-39-1"><a id="__codelineno-39-1" name="__codelineno-39-1" href="#__codelineno-39-1"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-39-2"><a id="__codelineno-39-2" name="__codelineno-39-2" href="#__codelineno-39-2"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>USERNAME<span class="k">)</span>
</span><span id="__span-39-3"><a id="__codelineno-39-3" name="__codelineno-39-3" href="#__codelineno-39-3"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>JAVA_HOME<span class="k">)</span>
</span><span id="__span-39-4"><a id="__codelineno-39-4" name="__codelineno-39-4" href="#__codelineno-39-4"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>SystemRoot<span class="k">)</span>
</span></code></pre></div>
<h3 id="_28">变量替换引用<a class="headerlink" href="#_28" title="Permanent link">&para;</a></h3>
<p>语法：<strong>$(var:a=b)</strong>，意思是将变量var的值当中每一项结尾的a替换为b，直接上例子</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-40-1"><a id="__codelineno-40-1" name="__codelineno-40-1" href="#__codelineno-40-1"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>main.cpp<span class="w"> </span>hello.cpp
</span><span id="__span-40-2"><a id="__codelineno-40-2" name="__codelineno-40-2" href="#__codelineno-40-2"></a><span class="nv">objs</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$(</span>files:.cpp<span class="o">=</span>.o<span class="k">)</span><span class="w"> </span><span class="c1"># main.o hello.o</span>
</span><span id="__span-40-3"><a id="__codelineno-40-3" name="__codelineno-40-3" href="#__codelineno-40-3"></a><span class="c"># 另一种写法</span>
</span><span id="__span-40-4"><a id="__codelineno-40-4" name="__codelineno-40-4" href="#__codelineno-40-4"></a><span class="nv">objs</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$(</span>files:%.cpp<span class="o">=</span>%.o<span class="k">)</span>
</span></code></pre></div>
<h3 id="_29">变量覆盖<a class="headerlink" href="#_29" title="Permanent link">&para;</a></h3>
<p>所有在Makefile中的变量，都可以在执行make时能过指定参数的方式进行覆盖。</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-41-1"><a id="__codelineno-41-1" name="__codelineno-41-1" href="#__codelineno-41-1"></a><span class="nv">OverridDemo</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span>ThisIsInMakefile
</span><span id="__span-41-2"><a id="__codelineno-41-2" name="__codelineno-41-2" href="#__codelineno-41-2"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-41-3"><a id="__codelineno-41-3" name="__codelineno-41-3" href="#__codelineno-41-3"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>OverridDemo<span class="k">)</span>
</span></code></pre></div>
<p>如果直接执行</p>
<div class="language-shell highlight"><pre><span></span><code><span id="__span-42-1"><a id="__codelineno-42-1" name="__codelineno-42-1" href="#__codelineno-42-1"></a>make
</span></code></pre></div>
<p>则上面的输出内容为*ThisIsInMakefile*，但可以在执行make时指定参数：</p>
<div class="language-shell highlight"><pre><span></span><code><span id="__span-43-1"><a id="__codelineno-43-1" name="__codelineno-43-1" href="#__codelineno-43-1"></a>make<span class="w"> </span><span class="nv">OverridDemo</span><span class="o">=</span>ThisIsFromOutShell<span class="w"> </span><span class="c1"># 等号两边不能有空格</span>
</span><span id="__span-43-2"><a id="__codelineno-43-2" name="__codelineno-43-2" href="#__codelineno-43-2"></a><span class="c1"># 如果变量值中有空格，需要用引号</span>
</span><span id="__span-43-3"><a id="__codelineno-43-3" name="__codelineno-43-3" href="#__codelineno-43-3"></a>make<span class="w"> </span><span class="nv">OverridDemo</span><span class="o">=</span>“This<span class="w"> </span>Is<span class="w"> </span>From<span class="w"> </span>Out<span class="w"> </span>Shell”
</span></code></pre></div>
<p>则输出OverridDemo的值是ThisIsFromOutShell或This Is From Out Shell。</p>
<p>用这样的命令参数会覆盖Makefile中对应变量的值，如果不想被覆盖，可以在变量前加上override指令，override具有较高优先级，不会被命令参数覆盖</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-44-1"><a id="__codelineno-44-1" name="__codelineno-44-1" href="#__codelineno-44-1"></a><span class="nf">override OverridDemo </span><span class="o">:</span>= <span class="n">ThisIsInMakefile</span>
</span><span id="__span-44-2"><a id="__codelineno-44-2" name="__codelineno-44-2" href="#__codelineno-44-2"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-44-3"><a id="__codelineno-44-3" name="__codelineno-44-3" href="#__codelineno-44-3"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>OverridDemo<span class="k">)</span>
</span></code></pre></div>
<p>这样即使命令行指定参数</p>
<div class="language-shell highlight"><pre><span></span><code><span id="__span-45-1"><a id="__codelineno-45-1" name="__codelineno-45-1" href="#__codelineno-45-1"></a>make<span class="w"> </span><span class="nv">OverridDemo</span><span class="o">=</span>ThisIsFromOutShell
</span></code></pre></div>
<p>输出结果依然是ThisIsInMakefile</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3 id="_30">自动变量<a class="headerlink" href="#_30" title="Permanent link">&para;</a></h3>
<p><strong>$@</strong>：①本条规则的目标名；②如果目标是归档文件的成员，则为归档文件名；③在多目标的模式规则中, 为导致本条规则方法执行的那个目标名；</p>
<p><strong>$&lt;</strong>：本条规则的第一个依赖名称</p>
<p><strong>$?</strong>：依赖中修改时间晚于目标文件修改时间的所有文件名，以空格隔开</p>
<p><strong>$^</strong>：所有依赖文件名，文件名不会重复，不包含order-only依赖</p>
<p><strong>$+</strong>：类似上一个， 表示所有依赖文件名，包括重复的文件名，不包含order-only依赖</p>
<p><strong>$|</strong>：所有order-only依赖文件名</p>
<p><strong>$*</strong>：(简单理解)目标文件名的主干部分(即不包括后缀名)</p>
<p><strong>$%</strong>：如果目标不是归档文件，则为空；如果目标是归档文件成员，则为对应的成员文件名</p>
<p>&nbsp;</p>
<p>以下变量对应上述变量，D为对应变量所在的目录，结尾不带/，F为对应变量除去目录部分的文件名</p>
<p><strong>$(@D)</strong></p>
<p><strong>$(@F)</strong></p>
<p><strong>$(*D)</strong></p>
<p><strong>$(*F)</strong></p>
<p><strong>$(%D)</strong></p>
<p><strong>$(%F)</strong></p>
<p><strong>$(&lt;D)</strong></p>
<p><strong>$(&lt;F)</strong></p>
<p><strong>$(^D)</strong></p>
<p><strong>$(^F)</strong></p>
<p><strong>$(+D)</strong></p>
<p><strong>$(+F)</strong></p>
<p><strong>$(?D)</strong></p>
<p><strong>$(?F)</strong></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3 id="_31">绑定目标的变量<a class="headerlink" href="#_31" title="Permanent link">&para;</a></h3>
<p>Makefile中的变量一般是全局变量。也就是说定义之后在Makefile的任意位置都可以使用。但也可以将变量指定在某个目标的范围内，这样这个变量就只能在这个目标对应的规则里面保用</p>
<p>语法</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-46-1"><a id="__codelineno-46-1" name="__codelineno-46-1" href="#__codelineno-46-1"></a><span class="nf">target … </span><span class="o">:</span><span class="w"> </span><span class="n">variable</span>-<span class="n">assignment</span>
</span><span id="__span-46-2"><a id="__codelineno-46-2" name="__codelineno-46-2" href="#__codelineno-46-2"></a><span class="nf">target … </span><span class="o">:</span><span class="w"> </span><span class="n">prerequisites</span>
</span><span id="__span-46-3"><a id="__codelineno-46-3" name="__codelineno-46-3" href="#__codelineno-46-3"></a><span class="w">    </span>recipes
</span><span id="__span-46-4"><a id="__codelineno-46-4" name="__codelineno-46-4" href="#__codelineno-46-4"></a><span class="w">    </span>…
</span></code></pre></div>
<p>例</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-47-1"><a id="__codelineno-47-1" name="__codelineno-47-1" href="#__codelineno-47-1"></a><span class="nv">var1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Global<span class="w"> </span>Var
</span><span id="__span-47-2"><a id="__codelineno-47-2" name="__codelineno-47-2" href="#__codelineno-47-2"></a>
</span><span id="__span-47-3"><a id="__codelineno-47-3" name="__codelineno-47-3" href="#__codelineno-47-3"></a><span class="nf">first</span><span class="o">:</span><span class="w"> </span><span class="n">all</span> <span class="n">t</span>2
</span><span id="__span-47-4"><a id="__codelineno-47-4" name="__codelineno-47-4" href="#__codelineno-47-4"></a>
</span><span id="__span-47-5"><a id="__codelineno-47-5" name="__codelineno-47-5" href="#__codelineno-47-5"></a><span class="nf">all</span><span class="o">:</span><span class="w"> </span><span class="n">var</span>2 = <span class="n">Target</span> <span class="n">All</span> <span class="n">Var</span>
</span><span id="__span-47-6"><a id="__codelineno-47-6" name="__codelineno-47-6" href="#__codelineno-47-6"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-47-7"><a id="__codelineno-47-7" name="__codelineno-47-7" href="#__codelineno-47-7"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>var1<span class="k">)</span>
</span><span id="__span-47-8"><a id="__codelineno-47-8" name="__codelineno-47-8" href="#__codelineno-47-8"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>var2<span class="k">)</span>
</span><span id="__span-47-9"><a id="__codelineno-47-9" name="__codelineno-47-9" href="#__codelineno-47-9"></a>
</span><span id="__span-47-10"><a id="__codelineno-47-10" name="__codelineno-47-10" href="#__codelineno-47-10"></a><span class="nf">t2</span><span class="o">:</span>
</span><span id="__span-47-11"><a id="__codelineno-47-11" name="__codelineno-47-11" href="#__codelineno-47-11"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>var1<span class="k">)</span>
</span><span id="__span-47-12"><a id="__codelineno-47-12" name="__codelineno-47-12" href="#__codelineno-47-12"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>var2<span class="k">)</span>
</span></code></pre></div>
<p>这种定义变量的方式，目标也可以使用模式匹配，这样所有能匹配上的目标范围内都可以使用这些变量</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-48-1"><a id="__codelineno-48-1" name="__codelineno-48-1" href="#__codelineno-48-1"></a><span class="nv">var1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Global<span class="w"> </span>Var
</span><span id="__span-48-2"><a id="__codelineno-48-2" name="__codelineno-48-2" href="#__codelineno-48-2"></a>
</span><span id="__span-48-3"><a id="__codelineno-48-3" name="__codelineno-48-3" href="#__codelineno-48-3"></a><span class="nf">first</span><span class="o">:</span><span class="w"> </span><span class="n">all</span>.<span class="n">v</span> <span class="n">t</span>2.<span class="n">v</span> <span class="n">t</span>3
</span><span id="__span-48-4"><a id="__codelineno-48-4" name="__codelineno-48-4" href="#__codelineno-48-4"></a>
</span><span id="__span-48-5"><a id="__codelineno-48-5" name="__codelineno-48-5" href="#__codelineno-48-5"></a><span class="nf">%.v</span><span class="o">:</span><span class="w"> </span><span class="n">var</span>2 = <span class="n">Target</span> %.<span class="n">v</span> <span class="n">Var</span>
</span><span id="__span-48-6"><a id="__codelineno-48-6" name="__codelineno-48-6" href="#__codelineno-48-6"></a><span class="nf">all.v</span><span class="o">:</span>
</span><span id="__span-48-7"><a id="__codelineno-48-7" name="__codelineno-48-7" href="#__codelineno-48-7"></a><span class="w">    </span>@echo<span class="w"> </span><span class="nv">$@</span><span class="w"> </span>--<span class="w"> </span><span class="k">$(</span>var1<span class="k">)</span>
</span><span id="__span-48-8"><a id="__codelineno-48-8" name="__codelineno-48-8" href="#__codelineno-48-8"></a><span class="w">    </span>@echo<span class="w"> </span><span class="nv">$@</span><span class="w"> </span>--<span class="w"> </span><span class="k">$(</span>var2<span class="k">)</span>
</span><span id="__span-48-9"><a id="__codelineno-48-9" name="__codelineno-48-9" href="#__codelineno-48-9"></a>
</span><span id="__span-48-10"><a id="__codelineno-48-10" name="__codelineno-48-10" href="#__codelineno-48-10"></a><span class="nf">t2.v</span><span class="o">:</span>
</span><span id="__span-48-11"><a id="__codelineno-48-11" name="__codelineno-48-11" href="#__codelineno-48-11"></a><span class="w">    </span>@echo<span class="w"> </span><span class="nv">$@</span><span class="w"> </span>--<span class="w"> </span><span class="k">$(</span>var1<span class="k">)</span>
</span><span id="__span-48-12"><a id="__codelineno-48-12" name="__codelineno-48-12" href="#__codelineno-48-12"></a><span class="w">    </span>@echo<span class="w"> </span><span class="nv">$@</span><span class="w"> </span>--<span class="w"> </span><span class="k">$(</span>var2<span class="k">)</span>
</span><span id="__span-48-13"><a id="__codelineno-48-13" name="__codelineno-48-13" href="#__codelineno-48-13"></a><span class="nf">t3</span><span class="o">:</span>
</span><span id="__span-48-14"><a id="__codelineno-48-14" name="__codelineno-48-14" href="#__codelineno-48-14"></a><span class="w">    </span>@echo<span class="w"> </span><span class="nv">$@</span><span class="w"> </span>--<span class="w"> </span><span class="k">$(</span>var1<span class="k">)</span>
</span><span id="__span-48-15"><a id="__codelineno-48-15" name="__codelineno-48-15" href="#__codelineno-48-15"></a><span class="w">    </span>@echo<span class="w"> </span><span class="nv">$@</span><span class="w"> </span>--<span class="w"> </span><span class="k">$(</span>var2<span class="k">)</span>
</span></code></pre></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3 id="_32">二次展开<a class="headerlink" href="#_32" title="Permanent link">&para;</a></h3>
<p>前面说过依赖中的变量都是在Makefile读取阶段立即展开的。如果想让依赖的的变量延迟展开，可以使用.SECONDEXPANSION:，添加之后，在依赖中使用变量时用<code>$$</code>，可以让变量在第二阶段进行二次展开，从而达到延迟展开的效果。</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-49-1"><a id="__codelineno-49-1" name="__codelineno-49-1" href="#__codelineno-49-1"></a><span class="nv">VAR1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>main.cpp
</span><span id="__span-49-2"><a id="__codelineno-49-2" name="__codelineno-49-2" href="#__codelineno-49-2"></a><span class="nf">.SECONDEXPANSION</span><span class="o">:</span>
</span><span id="__span-49-3"><a id="__codelineno-49-3" name="__codelineno-49-3" href="#__codelineno-49-3"></a><span class="nf">all</span><span class="o">:</span><span class="w"> </span>$<span class="k">$(</span><span class="nv">VAR1</span><span class="k">)</span>
</span><span id="__span-49-4"><a id="__codelineno-49-4" name="__codelineno-49-4" href="#__codelineno-49-4"></a><span class="w">    </span>@echo<span class="w"> </span>$^
</span><span id="__span-49-5"><a id="__codelineno-49-5" name="__codelineno-49-5" href="#__codelineno-49-5"></a>
</span><span id="__span-49-6"><a id="__codelineno-49-6" name="__codelineno-49-6" href="#__codelineno-49-6"></a><span class="nv">VAR1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello.cpp
</span></code></pre></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h1 id="_33">自动推导与隐式规则<a class="headerlink" href="#_33" title="Permanent link">&para;</a></h1>
<p>Makefile中有一些生成目标文件的规则使用频率非常高，比如由.c或.cpp文件编译成.o文件，这样的规则在make中可以自动推导，所以可以不用明确写出来，这样的规则称为隐式规则。</p>
<h2 id="make_1">一些make预定义的规则<a class="headerlink" href="#make_1" title="Permanent link">&para;</a></h2>
<h3 id="c">C语言编译<a class="headerlink" href="#c" title="Permanent link">&para;</a></h3>
<p>从.c到.o</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-50-1"><a id="__codelineno-50-1" name="__codelineno-50-1" href="#__codelineno-50-1"></a><span class="k">$(</span><span class="nv">CC</span><span class="k">)</span><span class="w"> </span><span class="k">$(</span><span class="nv">CPPFLAGS</span><span class="k">)</span><span class="w"> </span><span class="k">$(</span><span class="nv">CFLAGS</span><span class="k">)</span><span class="w"> </span><span class="err">-c</span>
</span></code></pre></div>
<h3 id="c_1">C++编译<a class="headerlink" href="#c_1" title="Permanent link">&para;</a></h3>
<p>从.cc .cpp .C到.o</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-51-1"><a id="__codelineno-51-1" name="__codelineno-51-1" href="#__codelineno-51-1"></a><span class="k">$(</span><span class="nv">CXX</span><span class="k">)</span><span class="w"> </span><span class="k">$(</span><span class="nv">CPPFLAGS</span><span class="k">)</span><span class="w"> </span><span class="k">$(</span><span class="nv">CXXFLAGS</span><span class="k">)</span><span class="w"> </span><span class="err">-c</span>
</span></code></pre></div>
<h3 id="_34">链接<a class="headerlink" href="#_34" title="Permanent link">&para;</a></h3>
<p>由.o文件链接到可执行文件</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-52-1"><a id="__codelineno-52-1" name="__codelineno-52-1" href="#__codelineno-52-1"></a><span class="k">$(</span><span class="nv">CC</span><span class="k">)</span><span class="w"> </span><span class="k">$(</span><span class="nv">LDFLAGS</span><span class="k">)</span><span class="w"> </span><span class="err">*.o</span><span class="w"> </span><span class="k">$(</span><span class="nv">LOADLIBES</span><span class="k">)</span><span class="w"> </span><span class="k">$(</span><span class="nv">LDLIBS</span><span class="k">)</span>
</span></code></pre></div>
<h2 id="_35">隐式规则中常用一些变量<a class="headerlink" href="#_35" title="Permanent link">&para;</a></h2>
<p>这些变量都有默认值，也可以自行修改</p>
<h3 id="cc">CC<a class="headerlink" href="#cc" title="Permanent link">&para;</a></h3>
<p>编译C语言的程序，默认为 <code>cc</code></p>
<h3 id="cxx">CXX<a class="headerlink" href="#cxx" title="Permanent link">&para;</a></h3>
<p>编译C++的程序，默认为 <code>g++</code></p>
<h3 id="ar">AR<a class="headerlink" href="#ar" title="Permanent link">&para;</a></h3>
<p>归档程序，默认为 <code>ar</code></p>
<h3 id="cpp">CPP<a class="headerlink" href="#cpp" title="Permanent link">&para;</a></h3>
<p>C语言预处理程序，默认为 <code>$(CC) -E</code></p>
<h3 id="rm">RM<a class="headerlink" href="#rm" title="Permanent link">&para;</a></h3>
<p>删除文件的程序，默认为<code>rm -f</code></p>
<h3 id="cflags">CFLAGS<a class="headerlink" href="#cflags" title="Permanent link">&para;</a></h3>
<p>传递给C编译器的一些选项，如-O2 -Iinclude</p>
<h3 id="cxxflags">CXXFLAGS<a class="headerlink" href="#cxxflags" title="Permanent link">&para;</a></h3>
<p>传递给C++编译器的一些选项，如-std=c++ 11 -fexec-charset=GBK   </p>
<h3 id="cppflags">CPPFLAGS<a class="headerlink" href="#cppflags" title="Permanent link">&para;</a></h3>
<p>C语言预处理的一些选项</p>
<h3 id="ldflags">LDFLAGS<a class="headerlink" href="#ldflags" title="Permanent link">&para;</a></h3>
<p>链接选项，如-L.</p>
<h3 id="ldlibs">LDLIBS<a class="headerlink" href="#ldlibs" title="Permanent link">&para;</a></h3>
<p>链接需要用到的库，如-lkernel32 -luser32 -lgdi32</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h1 id="_36">多目标与多规则<a class="headerlink" href="#_36" title="Permanent link">&para;</a></h1>
<p>显式规则中一条规则可以有多个目标，多个目标可以是相互独立的目标，也可以是组合目标，用写法来区分</p>
<h2 id="_37">独立多目标<a class="headerlink" href="#_37" title="Permanent link">&para;</a></h2>
<p>相互独立的多个目标与依赖之间直接用<code>:</code>，常用这种方式的有以下两种情况</p>
<ol>
<li>只需要写目标和依赖，不需要写方法的时候</li>
</ol>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-53-1"><a id="__codelineno-53-1" name="__codelineno-53-1" href="#__codelineno-53-1"></a><span class="nf">block.o input.o scene.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span>
</span></code></pre></div>
<p>这种写法等价于</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-54-1"><a id="__codelineno-54-1" name="__codelineno-54-1" href="#__codelineno-54-1"></a><span class="nf">block.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span>
</span><span id="__span-54-2"><a id="__codelineno-54-2" name="__codelineno-54-2" href="#__codelineno-54-2"></a><span class="nf">input.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span>
</span><span id="__span-54-3"><a id="__codelineno-54-3" name="__codelineno-54-3" href="#__codelineno-54-3"></a><span class="nf">scene.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span>
</span></code></pre></div>
<ol>
<li>生成(更新)目标的方法写法一样的，只是依赖与目标不一样时。之前写的Makfile中，有如下代码：</li>
</ol>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-55-1"><a id="__codelineno-55-1" name="__codelineno-55-1" href="#__codelineno-55-1"></a><span class="nf">block.o</span><span class="o">:</span><span class="w"> </span><span class="n">block</span>.<span class="n">cpp</span> <span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">color</span>.<span class="n">h</span>
</span><span id="__span-55-2"><a id="__codelineno-55-2" name="__codelineno-55-2" href="#__codelineno-55-2"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>block.cpp
</span><span id="__span-55-3"><a id="__codelineno-55-3" name="__codelineno-55-3" href="#__codelineno-55-3"></a><span class="nf">command.o</span><span class="o">:</span><span class="w"> </span><span class="n">command</span>.<span class="n">cpp</span> <span class="n">command</span>.<span class="n">h</span> <span class="n">scene</span>.<span class="n">h</span>
</span><span id="__span-55-4"><a id="__codelineno-55-4" name="__codelineno-55-4" href="#__codelineno-55-4"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>command.cpp
</span><span id="__span-55-5"><a id="__codelineno-55-5" name="__codelineno-55-5" href="#__codelineno-55-5"></a><span class="nf">input.o</span><span class="o">:</span><span class="w"> </span><span class="n">input</span>.<span class="n">cpp</span> <span class="n">common</span>.<span class="n">h</span> <span class="n">utility</span>.<span class="n">inl</span>
</span><span id="__span-55-6"><a id="__codelineno-55-6" name="__codelineno-55-6" href="#__codelineno-55-6"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>input.cpp
</span><span id="__span-55-7"><a id="__codelineno-55-7" name="__codelineno-55-7" href="#__codelineno-55-7"></a><span class="nf">main.o</span><span class="o">:</span><span class="w"> </span><span class="n">main</span>.<span class="n">cpp</span> <span class="n">scene</span>.<span class="n">h</span> <span class="n">input</span>.<span class="n">h</span> <span class="n">test</span>.<span class="n">h</span>
</span><span id="__span-55-8"><a id="__codelineno-55-8" name="__codelineno-55-8" href="#__codelineno-55-8"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>main.cpp
</span><span id="__span-55-9"><a id="__codelineno-55-9" name="__codelineno-55-9" href="#__codelineno-55-9"></a><span class="nf">scene.o</span><span class="o">:</span><span class="w"> </span><span class="n">scene</span>.<span class="n">cpp</span> <span class="n">common</span>.<span class="n">h</span> <span class="n">scene</span>.<span class="n">h</span> <span class="n">utility</span>.<span class="n">inl</span>
</span><span id="__span-55-10"><a id="__codelineno-55-10" name="__codelineno-55-10" href="#__codelineno-55-10"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>scene.cpp
</span><span id="__span-55-11"><a id="__codelineno-55-11" name="__codelineno-55-11" href="#__codelineno-55-11"></a><span class="nf">test.o</span><span class="o">:</span><span class="w"> </span><span class="n">test</span>.<span class="n">cpp</span> <span class="n">test</span>.<span class="n">h</span>
</span><span id="__span-55-12"><a id="__codelineno-55-12" name="__codelineno-55-12" href="#__codelineno-55-12"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>test.cpp
</span></code></pre></div>
<p>所有.o文件的生成都用的同一方法</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-56-1"><a id="__codelineno-56-1" name="__codelineno-56-1" href="#__codelineno-56-1"></a><span class="err">g++</span><span class="w"> </span><span class="err">-c</span><span class="w"> </span><span class="err">&lt;文件名&gt;</span>
</span></code></pre></div>
<p>如果不考虑依赖源文件进行更新时，可以进行简写如下：</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-57-1"><a id="__codelineno-57-1" name="__codelineno-57-1" href="#__codelineno-57-1"></a><span class="nf">block.o command.o input.o main.o scene.o test.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">command</span>.<span class="n">h</span> ...
</span><span id="__span-57-2"><a id="__codelineno-57-2" name="__codelineno-57-2" href="#__codelineno-57-2"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span><span class="k">$(</span>@:%.o<span class="o">=</span>%.cpp<span class="k">)</span>
</span></code></pre></div>
<p>这种写法实际上等价于</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-58-1"><a id="__codelineno-58-1" name="__codelineno-58-1" href="#__codelineno-58-1"></a><span class="nf">block.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">command</span>.<span class="n">h</span> ...
</span><span id="__span-58-2"><a id="__codelineno-58-2" name="__codelineno-58-2" href="#__codelineno-58-2"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span><span class="k">$(</span>subst<span class="w"> </span>.o,.cpp,<span class="nv">$@</span><span class="k">)</span>
</span><span id="__span-58-3"><a id="__codelineno-58-3" name="__codelineno-58-3" href="#__codelineno-58-3"></a><span class="nf">command.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">command</span>.<span class="n">h</span> ...
</span><span id="__span-58-4"><a id="__codelineno-58-4" name="__codelineno-58-4" href="#__codelineno-58-4"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span><span class="k">$(</span>subst<span class="w"> </span>.o,.cpp,<span class="nv">$@</span><span class="k">)</span>
</span><span id="__span-58-5"><a id="__codelineno-58-5" name="__codelineno-58-5" href="#__codelineno-58-5"></a><span class="nf">input.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">command</span>.<span class="n">h</span> ...
</span><span id="__span-58-6"><a id="__codelineno-58-6" name="__codelineno-58-6" href="#__codelineno-58-6"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span><span class="k">$(</span>subst<span class="w"> </span>.o,.cpp,<span class="nv">$@</span><span class="k">)</span>
</span><span id="__span-58-7"><a id="__codelineno-58-7" name="__codelineno-58-7" href="#__codelineno-58-7"></a><span class="nf">main.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">command</span>.<span class="n">h</span> ...
</span><span id="__span-58-8"><a id="__codelineno-58-8" name="__codelineno-58-8" href="#__codelineno-58-8"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span><span class="k">$(</span>subst<span class="w"> </span>.o,.cpp,<span class="nv">$@</span><span class="k">)</span>
</span><span id="__span-58-9"><a id="__codelineno-58-9" name="__codelineno-58-9" href="#__codelineno-58-9"></a><span class="nf">scene.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">command</span>.<span class="n">h</span> ...
</span><span id="__span-58-10"><a id="__codelineno-58-10" name="__codelineno-58-10" href="#__codelineno-58-10"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span><span class="k">$(</span>subst<span class="w"> </span>.o,.cpp,<span class="nv">$@</span><span class="k">)</span>
</span><span id="__span-58-11"><a id="__codelineno-58-11" name="__codelineno-58-11" href="#__codelineno-58-11"></a><span class="nf">test.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span> <span class="n">block</span>.<span class="n">h</span> <span class="n">command</span>.<span class="n">h</span> ...
</span><span id="__span-58-12"><a id="__codelineno-58-12" name="__codelineno-58-12" href="#__codelineno-58-12"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span><span class="k">$(</span>subst<span class="w"> </span>.o,.cpp,<span class="nv">$@</span><span class="k">)</span>
</span></code></pre></div>
<p>其中，<span class="arithmatex"><span class="MathJax_Preview">@表示的是目标名称。subst是一个字符串替换函数，</span><script type="math/tex">@表示的是目标名称。subst是一个字符串替换函数，</script></span>(subst .o,.cpp,$@)表示将目标名称中的.o替换为.cpp。</p>
<p>这样的简写可以减少内容的书写量，但是不利于将每个目标与依赖分别对应。</p>
<p>独立多目标虽然写在一起，但是每个目标都是单独调用一次方法来更新的。和分开写效果一样。</p>
<h2 id="_38">组合多目标<a class="headerlink" href="#_38" title="Permanent link">&para;</a></h2>
<p>多目标与依赖之前用<code>&amp;:</code>，这样的多个目标称为组合目标。与独立多目标的区别在于，独立多目标每个目标的更新需要单独调用一次更新方法。而组合多目标调用一次方法将更新所有目标</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-59-1"><a id="__codelineno-59-1" name="__codelineno-59-1" href="#__codelineno-59-1"></a><span class="nf">block.o input.o scene.o &amp;</span><span class="o">:</span><span class="w"> </span><span class="n">block</span>.<span class="n">cpp</span> <span class="n">input</span>.<span class="n">cpp</span> <span class="n">scene</span>.<span class="n">cpp</span> <span class="n">common</span>.<span class="n">h</span>
</span><span id="__span-59-2"><a id="__codelineno-59-2" name="__codelineno-59-2" href="#__codelineno-59-2"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>block.cpp
</span><span id="__span-59-3"><a id="__codelineno-59-3" name="__codelineno-59-3" href="#__codelineno-59-3"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>input.cpp
</span><span id="__span-59-4"><a id="__codelineno-59-4" name="__codelineno-59-4" href="#__codelineno-59-4"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>scene.cpp
</span></code></pre></div>
<p>所有目标的更新方法都写到其中，每次更新只会调用一次。</p>
<h2 id="_39">同一目标多条规则<a class="headerlink" href="#_39" title="Permanent link">&para;</a></h2>
<p>同一目标可以对应多条规则。同一目标的所有规则中的依赖会被合并。但如果同一目标对应的多条规则都写了更新方法，则会使用最新的一条更新方法，并且会输出警告信息。</p>
<p>同一目标多规则通常用来给多个目标添加依赖而不用改动已写好的部分。</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-60-1"><a id="__codelineno-60-1" name="__codelineno-60-1" href="#__codelineno-60-1"></a><span class="nf">input.o</span><span class="o">:</span><span class="w"> </span><span class="n">input</span>.<span class="n">cpp</span> <span class="n">utility</span>.<span class="n">inl</span>
</span><span id="__span-60-2"><a id="__codelineno-60-2" name="__codelineno-60-2" href="#__codelineno-60-2"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>input.cpp
</span><span id="__span-60-3"><a id="__codelineno-60-3" name="__codelineno-60-3" href="#__codelineno-60-3"></a><span class="nf">main.o</span><span class="o">:</span><span class="w"> </span><span class="n">main</span>.<span class="n">cpp</span> <span class="n">scene</span>.<span class="n">h</span> <span class="n">input</span>.<span class="n">h</span> <span class="n">test</span>.<span class="n">h</span>
</span><span id="__span-60-4"><a id="__codelineno-60-4" name="__codelineno-60-4" href="#__codelineno-60-4"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>main.cpp
</span><span id="__span-60-5"><a id="__codelineno-60-5" name="__codelineno-60-5" href="#__codelineno-60-5"></a><span class="nf">scene.o</span><span class="o">:</span><span class="w"> </span><span class="n">scene</span>.<span class="n">cpp</span> <span class="n">scene</span>.<span class="n">h</span> <span class="n">utility</span>.<span class="n">inl</span>
</span><span id="__span-60-6"><a id="__codelineno-60-6" name="__codelineno-60-6" href="#__codelineno-60-6"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>scene.cpp
</span><span id="__span-60-7"><a id="__codelineno-60-7" name="__codelineno-60-7" href="#__codelineno-60-7"></a>
</span><span id="__span-60-8"><a id="__codelineno-60-8" name="__codelineno-60-8" href="#__codelineno-60-8"></a><span class="nf">input.o main.o scene.o </span><span class="o">:</span><span class="w"> </span><span class="n">common</span>.<span class="n">h</span>
</span></code></pre></div>
<p>同时给三个目标添加了一个依赖common.h，但是不用修改上面已写好的部分。</p>
<h1 id="_40">静态模式<a class="headerlink" href="#_40" title="Permanent link">&para;</a></h1>
<p>独立多目标可以简化Makefile的书写，但是不利于将各个目标的依赖分开，让目标文件根据各自的依赖进行更新。静态模式可以在一定程度上改进依赖分开问题。</p>
<p>静态模式就是用<code>%</code>进行文件匹配来推导出对应的依赖。</p>
<p><strong>语法</strong></p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-61-1"><a id="__codelineno-61-1" name="__codelineno-61-1" href="#__codelineno-61-1"></a><span class="nf">targets …</span><span class="o">:</span><span class="w"> </span><span class="n">target</span>-<span class="n">pattern</span>(目标模式): <span class="n">prereq</span>-<span class="n">patterns</span>(依赖模式) …
</span><span id="__span-61-2"><a id="__codelineno-61-2" name="__codelineno-61-2" href="#__codelineno-61-2"></a><span class="w">        </span>recipe
</span><span id="__span-61-3"><a id="__codelineno-61-3" name="__codelineno-61-3" href="#__codelineno-61-3"></a><span class="w">        </span>…
</span></code></pre></div>
<p>先看一个例子</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-62-1"><a id="__codelineno-62-1" name="__codelineno-62-1" href="#__codelineno-62-1"></a><span class="nf">block.o </span><span class="o">:</span><span class="w"> </span>%.<span class="n">o</span> : %.<span class="n">cpp</span> %.<span class="n">h</span>
</span><span id="__span-62-2"><a id="__codelineno-62-2" name="__codelineno-62-2" href="#__codelineno-62-2"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>$&lt;
</span></code></pre></div>
<p>block.o为目标，%.o为目标模式，%.cpp，%.h为依赖模式，对于这一条规则，%.o代表的是目标文件block.o，所以这里的%匹配的是block，因此，%.cpp表示block.cpp，%.h代表block.h，所以block.o : %.o : %.cpp %.h表示的意思同下面这种写法</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-63-1"><a id="__codelineno-63-1" name="__codelineno-63-1" href="#__codelineno-63-1"></a><span class="nf">block.o </span><span class="o">:</span><span class="w"> </span><span class="n">block</span>.<span class="n">cpp</span> <span class="n">block</span>.<span class="n">h</span>
</span></code></pre></div>
<p>自动推导出block.o依赖block.cpp和block.h。</p>
<p>另外，$&lt;表示目标的第一个依赖，在这条规则中，$&lt;表示block.cpp</p>
<p>对应的Makefile可以做如下改进</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-64-1"><a id="__codelineno-64-1" name="__codelineno-64-1" href="#__codelineno-64-1"></a><span class="nf">block.o command.o input.o scene.o test.o</span><span class="o">:</span><span class="w"> </span>%.<span class="n">o</span> : %.<span class="n">cpp</span> %.<span class="n">h</span>
</span><span id="__span-64-2"><a id="__codelineno-64-2" name="__codelineno-64-2" href="#__codelineno-64-2"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>$&lt;
</span><span id="__span-64-3"><a id="__codelineno-64-3" name="__codelineno-64-3" href="#__codelineno-64-3"></a><span class="nf">main.o</span><span class="o">:</span><span class="w"> </span><span class="n">main</span>.<span class="n">cpp</span> <span class="n">scene</span>.<span class="n">h</span> <span class="n">input</span>.<span class="n">h</span> <span class="n">test</span>.<span class="n">h</span>
</span><span id="__span-64-4"><a id="__codelineno-64-4" name="__codelineno-64-4" href="#__codelineno-64-4"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>main.cpp
</span></code></pre></div>
<p>用这种方式可以在简写的同时一定程度上解决各个目标对应的依赖问题。</p>
<p>(不属于静态模式的内容，隐式规则的内容)利用模式匹配可以直接将所有.cpp到.o文件的编译简写为如下</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-65-1"><a id="__codelineno-65-1" name="__codelineno-65-1" href="#__codelineno-65-1"></a><span class="nf">%.o </span><span class="o">:</span><span class="w"> </span>%.<span class="n">cpp</span> %.<span class="n">h</span>
</span><span id="__span-65-2"><a id="__codelineno-65-2" name="__codelineno-65-2" href="#__codelineno-65-2"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>$&lt;
</span></code></pre></div>
<h1 id="_41">条件判断<a class="headerlink" href="#_41" title="Permanent link">&para;</a></h1>
<p>使用条件指令可以让make执行或略过Makefile文件中的一些部分。</p>
<p><strong>ifdef</strong>  判断一个变量是已否定义</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-66-1"><a id="__codelineno-66-1" name="__codelineno-66-1" href="#__codelineno-66-1"></a><span class="nv">OS</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Linux
</span><span id="__span-66-2"><a id="__codelineno-66-2" name="__codelineno-66-2" href="#__codelineno-66-2"></a><span class="cp">ifdef Win</span>
</span><span id="__span-66-3"><a id="__codelineno-66-3" name="__codelineno-66-3" href="#__codelineno-66-3"></a><span class="w">    </span><span class="nv">OS</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Windows
</span><span id="__span-66-4"><a id="__codelineno-66-4" name="__codelineno-66-4" href="#__codelineno-66-4"></a><span class="cp">endif</span>
</span><span id="__span-66-5"><a id="__codelineno-66-5" name="__codelineno-66-5" href="#__codelineno-66-5"></a>
</span><span id="__span-66-6"><a id="__codelineno-66-6" name="__codelineno-66-6" href="#__codelineno-66-6"></a>
</span><span id="__span-66-7"><a id="__codelineno-66-7" name="__codelineno-66-7" href="#__codelineno-66-7"></a><span class="nv">OS</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Linux
</span><span id="__span-66-8"><a id="__codelineno-66-8" name="__codelineno-66-8" href="#__codelineno-66-8"></a><span class="cp">ifdef Win</span>
</span><span id="__span-66-9"><a id="__codelineno-66-9" name="__codelineno-66-9" href="#__codelineno-66-9"></a><span class="w">    </span><span class="nv">OS</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Windows
</span><span id="__span-66-10"><a id="__codelineno-66-10" name="__codelineno-66-10" href="#__codelineno-66-10"></a><span class="cp">else ifdef Mac</span>
</span><span id="__span-66-11"><a id="__codelineno-66-11" name="__codelineno-66-11" href="#__codelineno-66-11"></a><span class="w">    </span><span class="nv">OS</span><span class="o">=</span><span class="w"> </span>MacOS
</span><span id="__span-66-12"><a id="__codelineno-66-12" name="__codelineno-66-12" href="#__codelineno-66-12"></a><span class="cp">endif</span>
</span><span id="__span-66-13"><a id="__codelineno-66-13" name="__codelineno-66-13" href="#__codelineno-66-13"></a>
</span><span id="__span-66-14"><a id="__codelineno-66-14" name="__codelineno-66-14" href="#__codelineno-66-14"></a>
</span><span id="__span-66-15"><a id="__codelineno-66-15" name="__codelineno-66-15" href="#__codelineno-66-15"></a><span class="cp">ifdef Win</span>
</span><span id="__span-66-16"><a id="__codelineno-66-16" name="__codelineno-66-16" href="#__codelineno-66-16"></a><span class="w">    </span><span class="nv">OS</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Windows
</span><span id="__span-66-17"><a id="__codelineno-66-17" name="__codelineno-66-17" href="#__codelineno-66-17"></a><span class="cp">else ifdef Mac</span>
</span><span id="__span-66-18"><a id="__codelineno-66-18" name="__codelineno-66-18" href="#__codelineno-66-18"></a><span class="w">    </span><span class="nv">OS</span><span class="o">=</span><span class="w"> </span>MacOS
</span><span id="__span-66-19"><a id="__codelineno-66-19" name="__codelineno-66-19" href="#__codelineno-66-19"></a><span class="cp">else </span>
</span><span id="__span-66-20"><a id="__codelineno-66-20" name="__codelineno-66-20" href="#__codelineno-66-20"></a><span class="w">    </span><span class="nv">OS</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Linux
</span><span id="__span-66-21"><a id="__codelineno-66-21" name="__codelineno-66-21" href="#__codelineno-66-21"></a><span class="cp">endif</span>
</span></code></pre></div>
<p><strong>ifndef</strong> 判断一个变量是否没被定义</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-67-1"><a id="__codelineno-67-1" name="__codelineno-67-1" href="#__codelineno-67-1"></a><span class="cp">ifndef FLAGS</span>
</span><span id="__span-67-2"><a id="__codelineno-67-2" name="__codelineno-67-2" href="#__codelineno-67-2"></a><span class="w">    </span><span class="nv">FLAGS</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>-finput-charset<span class="o">=</span>utf-8
</span><span id="__span-67-3"><a id="__codelineno-67-3" name="__codelineno-67-3" href="#__codelineno-67-3"></a><span class="cp">endif</span>
</span></code></pre></div>
<p><strong>ifeq</strong> 判断两个值是否相等</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-68-1"><a id="__codelineno-68-1" name="__codelineno-68-1" href="#__codelineno-68-1"></a><span class="nv">version</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>.0
</span><span id="__span-68-2"><a id="__codelineno-68-2" name="__codelineno-68-2" href="#__codelineno-68-2"></a>
</span><span id="__span-68-3"><a id="__codelineno-68-3" name="__codelineno-68-3" href="#__codelineno-68-3"></a><span class="cp">ifeq ($(version),1.0)            # ifeq后一定要一个空格</span>
</span><span id="__span-68-4"><a id="__codelineno-68-4" name="__codelineno-68-4" href="#__codelineno-68-4"></a><span class="w">    </span>msg<span class="w"> </span>:<span class="o">=</span><span class="w"> </span>版本太旧了，请更新版本
</span><span id="__span-68-5"><a id="__codelineno-68-5" name="__codelineno-68-5" href="#__codelineno-68-5"></a><span class="cp">else ifeq ($(version), 3.0)</span>
</span><span id="__span-68-6"><a id="__codelineno-68-6" name="__codelineno-68-6" href="#__codelineno-68-6"></a><span class="w">    </span>msg<span class="w"> </span>:<span class="o">=</span><span class="w"> </span>版本太新了，也不行
</span><span id="__span-68-7"><a id="__codelineno-68-7" name="__codelineno-68-7" href="#__codelineno-68-7"></a><span class="cp">else</span>
</span><span id="__span-68-8"><a id="__codelineno-68-8" name="__codelineno-68-8" href="#__codelineno-68-8"></a><span class="w">    </span>msg<span class="w"> </span>:<span class="o">=</span><span class="w"> </span>版本可以用
</span><span id="__span-68-9"><a id="__codelineno-68-9" name="__codelineno-68-9" href="#__codelineno-68-9"></a><span class="cp">endif</span>
</span><span id="__span-68-10"><a id="__codelineno-68-10" name="__codelineno-68-10" href="#__codelineno-68-10"></a>
</span><span id="__span-68-11"><a id="__codelineno-68-11" name="__codelineno-68-11" href="#__codelineno-68-11"></a>
</span><span id="__span-68-12"><a id="__codelineno-68-12" name="__codelineno-68-12" href="#__codelineno-68-12"></a><span class="c"># 另外的写法</span>
</span><span id="__span-68-13"><a id="__codelineno-68-13" name="__codelineno-68-13" href="#__codelineno-68-13"></a><span class="nv">msg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Other
</span><span id="__span-68-14"><a id="__codelineno-68-14" name="__codelineno-68-14" href="#__codelineno-68-14"></a><span class="cp">ifeq &quot;$(OS)&quot; &quot;Windows_NT&quot;</span>
</span><span id="__span-68-15"><a id="__codelineno-68-15" name="__codelineno-68-15" href="#__codelineno-68-15"></a><span class="w">    </span><span class="nv">msg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>This<span class="w"> </span>is<span class="w"> </span>a<span class="w"> </span>Windows<span class="w"> </span>Platform
</span><span id="__span-68-16"><a id="__codelineno-68-16" name="__codelineno-68-16" href="#__codelineno-68-16"></a><span class="cp">endif</span>
</span><span id="__span-68-17"><a id="__codelineno-68-17" name="__codelineno-68-17" href="#__codelineno-68-17"></a>
</span><span id="__span-68-18"><a id="__codelineno-68-18" name="__codelineno-68-18" href="#__codelineno-68-18"></a><span class="cp">ifeq &#39;$(OS)&#39; &#39;Windows_NT&#39;</span>
</span><span id="__span-68-19"><a id="__codelineno-68-19" name="__codelineno-68-19" href="#__codelineno-68-19"></a>
</span><span id="__span-68-20"><a id="__codelineno-68-20" name="__codelineno-68-20" href="#__codelineno-68-20"></a><span class="cp">ifeq &#39;$(OS)&#39; &quot;Windows_NT&quot;</span>
</span></code></pre></div>
<p><strong>ifneq</strong> 判断两个值是否不等</p>
<p>用法及参数同ifeq，只是判断结果相反</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h1 id="_42">文本处理函数<a class="headerlink" href="#_42" title="Permanent link">&para;</a></h1>
<p>C语言中，函数调用方法是function(arguments)；但在Makefile中调用函数的写法不同</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-69-1"><a id="__codelineno-69-1" name="__codelineno-69-1" href="#__codelineno-69-1"></a><span class="k">$(</span><span class="nv">function</span> <span class="nv">arguments</span><span class="k">)</span><span class="w"> </span><span class="err">或</span><span class="w"> </span><span class="err">${function</span><span class="w"> </span><span class="err">arguments}</span>
</span><span id="__span-69-2"><a id="__codelineno-69-2" name="__codelineno-69-2" href="#__codelineno-69-2"></a><span class="k">$(</span><span class="nv">function</span> <span class="nv">arg1</span>,<span class="k">$(</span><span class="nv">arg2</span><span class="k">)</span>,<span class="nv">arg3</span> <span class="nv">...</span><span class="k">)</span><span class="w">  </span><span class="c"># 参数之间不要有空格</span>
</span></code></pre></div>
<h2 id="_43">字符替换与分析<a class="headerlink" href="#_43" title="Permanent link">&para;</a></h2>
<h4 id="subst"><strong>subst</strong><a class="headerlink" href="#subst" title="Permanent link">&para;</a></h4>
<p>文本替换函数，返回替换后的文本</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-70-1"><a id="__codelineno-70-1" name="__codelineno-70-1" href="#__codelineno-70-1"></a><span class="k">$(</span><span class="nv">subst</span> <span class="nv">target</span>,<span class="nv">replacement</span>,<span class="nv">text</span><span class="k">)</span>
</span><span id="__span-70-2"><a id="__codelineno-70-2" name="__codelineno-70-2" href="#__codelineno-70-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">用relacement替换text中的target</span>
</span><span id="__span-70-3"><a id="__codelineno-70-3" name="__codelineno-70-3" href="#__codelineno-70-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">target</span><span class="w"> </span><span class="err">需要替换的内容</span>
</span><span id="__span-70-4"><a id="__codelineno-70-4" name="__codelineno-70-4" href="#__codelineno-70-4"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">replacement</span><span class="w"> </span><span class="err">替换为的内容</span>
</span><span id="__span-70-5"><a id="__codelineno-70-5" name="__codelineno-70-5" href="#__codelineno-70-5"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">text</span><span class="w"> </span><span class="err">需要处理的内容，可以是任意字符串</span>
</span><span id="__span-70-6"><a id="__codelineno-70-6" name="__codelineno-70-6" href="#__codelineno-70-6"></a>
</span><span id="__span-70-7"><a id="__codelineno-70-7" name="__codelineno-70-7" href="#__codelineno-70-7"></a>
</span><span id="__span-70-8"><a id="__codelineno-70-8" name="__codelineno-70-8" href="#__codelineno-70-8"></a>
</span><span id="__span-70-9"><a id="__codelineno-70-9" name="__codelineno-70-9" href="#__codelineno-70-9"></a><span class="nv">objs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>main.o<span class="w"> </span>hello.o
</span><span id="__span-70-10"><a id="__codelineno-70-10" name="__codelineno-70-10" href="#__codelineno-70-10"></a><span class="nv">srcs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>subst<span class="w"> </span>.o,.cpp,<span class="k">$(</span>objs<span class="k">))</span>
</span><span id="__span-70-11"><a id="__codelineno-70-11" name="__codelineno-70-11" href="#__codelineno-70-11"></a><span class="nv">headers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>subst<span class="w"> </span>.cpp,.h,<span class="k">$(</span>srcs<span class="k">))</span>
</span><span id="__span-70-12"><a id="__codelineno-70-12" name="__codelineno-70-12" href="#__codelineno-70-12"></a>
</span><span id="__span-70-13"><a id="__codelineno-70-13" name="__codelineno-70-13" href="#__codelineno-70-13"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-70-14"><a id="__codelineno-70-14" name="__codelineno-70-14" href="#__codelineno-70-14"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>srcs<span class="k">)</span>
</span><span id="__span-70-15"><a id="__codelineno-70-15" name="__codelineno-70-15" href="#__codelineno-70-15"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>headers<span class="k">)</span>
</span></code></pre></div>
<p><strong>patsubst</strong></p>
<p>模式替换， 返回替换后的文本</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-71-1"><a id="__codelineno-71-1" name="__codelineno-71-1" href="#__codelineno-71-1"></a><span class="k">$(</span><span class="nv">patsubst</span> <span class="nv">pattern</span>,<span class="nv">replacement</span>,<span class="nv">text</span><span class="k">)</span>
</span><span id="__span-71-2"><a id="__codelineno-71-2" name="__codelineno-71-2" href="#__codelineno-71-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">pattern</span><span class="w"> </span><span class="err">需要替换的模式</span>
</span><span id="__span-71-3"><a id="__codelineno-71-3" name="__codelineno-71-3" href="#__codelineno-71-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">replacement</span><span class="w"> </span><span class="err">需要替换为</span>
</span><span id="__span-71-4"><a id="__codelineno-71-4" name="__codelineno-71-4" href="#__codelineno-71-4"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">text</span><span class="w"> </span><span class="err">待处理内容，各项内容需要用空格隔开</span>
</span><span id="__span-71-5"><a id="__codelineno-71-5" name="__codelineno-71-5" href="#__codelineno-71-5"></a>
</span><span id="__span-71-6"><a id="__codelineno-71-6" name="__codelineno-71-6" href="#__codelineno-71-6"></a>
</span><span id="__span-71-7"><a id="__codelineno-71-7" name="__codelineno-71-7" href="#__codelineno-71-7"></a><span class="nv">objs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>main.ohello.o
</span><span id="__span-71-8"><a id="__codelineno-71-8" name="__codelineno-71-8" href="#__codelineno-71-8"></a><span class="nv">srcs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>subst<span class="w"> </span>%.o,%.cpp,<span class="k">$(</span>objs<span class="k">))</span>
</span><span id="__span-71-9"><a id="__codelineno-71-9" name="__codelineno-71-9" href="#__codelineno-71-9"></a><span class="nv">headers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>subst<span class="w"> </span>%.cpp,%.h,<span class="k">$(</span>srcs<span class="k">))</span><span class="w">    </span>
</span></code></pre></div>
<h4 id="strip"><strong>strip</strong><a class="headerlink" href="#strip" title="Permanent link">&para;</a></h4>
<p>去除字符串头部和尾部的空格，中间如果连续有多个空格，则用一个空格替换，返回去除空格后的文本</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-72-1"><a id="__codelineno-72-1" name="__codelineno-72-1" href="#__codelineno-72-1"></a><span class="k">$(</span><span class="nv">strip</span> <span class="nv">string</span><span class="k">)</span>
</span><span id="__span-72-2"><a id="__codelineno-72-2" name="__codelineno-72-2" href="#__codelineno-72-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">string</span><span class="w"> </span><span class="err">需要去除空格的字符串</span>
</span><span id="__span-72-3"><a id="__codelineno-72-3" name="__codelineno-72-3" href="#__codelineno-72-3"></a>
</span><span id="__span-72-4"><a id="__codelineno-72-4" name="__codelineno-72-4" href="#__codelineno-72-4"></a>
</span><span id="__span-72-5"><a id="__codelineno-72-5" name="__codelineno-72-5" href="#__codelineno-72-5"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>aa<span class="w"> </span>hello.cpp<span class="w">      </span>main.cpp<span class="w">     </span>test.cpp
</span><span id="__span-72-6"><a id="__codelineno-72-6" name="__codelineno-72-6" href="#__codelineno-72-6"></a><span class="nv">files</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$(</span>subst<span class="w"> </span>aa,<span class="w">        </span>,<span class="k">$(</span>files<span class="k">))</span>
</span><span id="__span-72-7"><a id="__codelineno-72-7" name="__codelineno-72-7" href="#__codelineno-72-7"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>strip<span class="w"> </span><span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="findstring">findstring<a class="headerlink" href="#findstring" title="Permanent link">&para;</a></h4>
<p>查找字符串，如果找到了，则返回对应的字符串，如果没找到，则反回空串</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-73-1"><a id="__codelineno-73-1" name="__codelineno-73-1" href="#__codelineno-73-1"></a><span class="k">$(</span><span class="nv">findstring</span> <span class="nv">find</span>,<span class="nv">string</span><span class="k">)</span>
</span><span id="__span-73-2"><a id="__codelineno-73-2" name="__codelineno-73-2" href="#__codelineno-73-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">find</span><span class="w"> </span><span class="err">需要查找的字符串</span>
</span><span id="__span-73-3"><a id="__codelineno-73-3" name="__codelineno-73-3" href="#__codelineno-73-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">string</span><span class="w"> </span><span class="err">用来查找的内容</span>
</span><span id="__span-73-4"><a id="__codelineno-73-4" name="__codelineno-73-4" href="#__codelineno-73-4"></a>
</span><span id="__span-73-5"><a id="__codelineno-73-5" name="__codelineno-73-5" href="#__codelineno-73-5"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>test.cpp
</span><span id="__span-73-6"><a id="__codelineno-73-6" name="__codelineno-73-6" href="#__codelineno-73-6"></a><span class="nv">find</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>findstring<span class="w"> </span>hel,<span class="k">$(</span>files<span class="k">))</span>
</span><span id="__span-73-7"><a id="__codelineno-73-7" name="__codelineno-73-7" href="#__codelineno-73-7"></a><span class="nv">find</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>findstring<span class="w"> </span>HEL,<span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="filter">filter<a class="headerlink" href="#filter" title="Permanent link">&para;</a></h4>
<p>从文本中筛选出符合模式的内容并返回</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-74-1"><a id="__codelineno-74-1" name="__codelineno-74-1" href="#__codelineno-74-1"></a><span class="k">$(</span><span class="nv">filter</span> <span class="nv">pattern</span>…,<span class="nv">text</span><span class="k">)</span>
</span><span id="__span-74-2"><a id="__codelineno-74-2" name="__codelineno-74-2" href="#__codelineno-74-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">pattern</span><span class="w"> </span><span class="err">模式，可以有多个，用空格隔开</span>
</span><span id="__span-74-3"><a id="__codelineno-74-3" name="__codelineno-74-3" href="#__codelineno-74-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">text</span><span class="w"> </span><span class="err">用来筛选的文本，多项内容需要用空格隔开，否则只会当一项来处理</span>
</span><span id="__span-74-4"><a id="__codelineno-74-4" name="__codelineno-74-4" href="#__codelineno-74-4"></a>
</span><span id="__span-74-5"><a id="__codelineno-74-5" name="__codelineno-74-5" href="#__codelineno-74-5"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>test.cpp<span class="w"> </span>main.o<span class="w"> </span>hello.o<span class="w"> </span>hello.h
</span><span id="__span-74-6"><a id="__codelineno-74-6" name="__codelineno-74-6" href="#__codelineno-74-6"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>filter<span class="w"> </span>%.o<span class="w"> </span>%.h,<span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="filter-out">filter-out<a class="headerlink" href="#filter-out" title="Permanent link">&para;</a></h4>
<p>与filter相反，过滤掉符合模式的，返回剩下的内容</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-75-1"><a id="__codelineno-75-1" name="__codelineno-75-1" href="#__codelineno-75-1"></a><span class="k">$(</span><span class="nv">filter-out</span> <span class="nv">pattern</span>…,<span class="nv">text</span><span class="k">)</span>
</span><span id="__span-75-2"><a id="__codelineno-75-2" name="__codelineno-75-2" href="#__codelineno-75-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">pattern</span><span class="w"> </span><span class="err">模式，可以有多个，用空格隔开</span>
</span><span id="__span-75-3"><a id="__codelineno-75-3" name="__codelineno-75-3" href="#__codelineno-75-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">text</span><span class="w"> </span><span class="err">用来筛选的文本，多项内容需要用空格隔开，否则只会当一项来处理</span>
</span><span id="__span-75-4"><a id="__codelineno-75-4" name="__codelineno-75-4" href="#__codelineno-75-4"></a>
</span><span id="__span-75-5"><a id="__codelineno-75-5" name="__codelineno-75-5" href="#__codelineno-75-5"></a>
</span><span id="__span-75-6"><a id="__codelineno-75-6" name="__codelineno-75-6" href="#__codelineno-75-6"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>test.cpp<span class="w"> </span>main.o<span class="w"> </span>hello.o<span class="w"> </span>hello.h
</span><span id="__span-75-7"><a id="__codelineno-75-7" name="__codelineno-75-7" href="#__codelineno-75-7"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>filter-out<span class="w"> </span>%.o<span class="w"> </span>%.cpp,<span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="sort">sort<a class="headerlink" href="#sort" title="Permanent link">&para;</a></h4>
<p>将文本内的各项按字典顺序排列，并且移除重复项</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-76-1"><a id="__codelineno-76-1" name="__codelineno-76-1" href="#__codelineno-76-1"></a><span class="k">$(</span><span class="nv">sort</span> <span class="nv">list</span><span class="k">)</span>
</span><span id="__span-76-2"><a id="__codelineno-76-2" name="__codelineno-76-2" href="#__codelineno-76-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">list</span><span class="w"> </span><span class="err">需要排序内容</span>
</span><span id="__span-76-3"><a id="__codelineno-76-3" name="__codelineno-76-3" href="#__codelineno-76-3"></a>
</span><span id="__span-76-4"><a id="__codelineno-76-4" name="__codelineno-76-4" href="#__codelineno-76-4"></a>
</span><span id="__span-76-5"><a id="__codelineno-76-5" name="__codelineno-76-5" href="#__codelineno-76-5"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>test.cpp<span class="w"> </span>main.o<span class="w"> </span>hello.o<span class="w"> </span>hello.h<span class="w"> </span>main.cpp<span class="w"> </span>hello.cpp
</span><span id="__span-76-6"><a id="__codelineno-76-6" name="__codelineno-76-6" href="#__codelineno-76-6"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>sort<span class="w"> </span><span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="word">word<a class="headerlink" href="#word" title="Permanent link">&para;</a></h4>
<p>用于返回文本中第n个单词</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-77-1"><a id="__codelineno-77-1" name="__codelineno-77-1" href="#__codelineno-77-1"></a><span class="k">$(</span><span class="nv">word</span> <span class="nv">n</span>,<span class="nv">text</span><span class="k">)</span>
</span><span id="__span-77-2"><a id="__codelineno-77-2" name="__codelineno-77-2" href="#__codelineno-77-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">n</span><span class="w"> </span><span class="err">第n个单词，从1开始，如果n大于总单词数，则返回空串</span>
</span><span id="__span-77-3"><a id="__codelineno-77-3" name="__codelineno-77-3" href="#__codelineno-77-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">text</span><span class="w"> </span><span class="err">待处理文本</span>
</span><span id="__span-77-4"><a id="__codelineno-77-4" name="__codelineno-77-4" href="#__codelineno-77-4"></a>
</span><span id="__span-77-5"><a id="__codelineno-77-5" name="__codelineno-77-5" href="#__codelineno-77-5"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>test.cpp<span class="w"> </span>main.o<span class="w"> </span>hello.o<span class="w"> </span>hello.h<span class="w"> </span>main.cpp<span class="w"> </span>hello.cpp
</span><span id="__span-77-6"><a id="__codelineno-77-6" name="__codelineno-77-6" href="#__codelineno-77-6"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>word<span class="w"> </span><span class="m">3</span>,<span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="wordlist">wordlist<a class="headerlink" href="#wordlist" title="Permanent link">&para;</a></h4>
<p>用于返回文本指定范围内的单词列表</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-78-1"><a id="__codelineno-78-1" name="__codelineno-78-1" href="#__codelineno-78-1"></a><span class="k">$(</span><span class="nv">wordlist</span> <span class="nv">start</span>,<span class="nv">end</span>,<span class="nv">text</span><span class="k">)</span>
</span><span id="__span-78-2"><a id="__codelineno-78-2" name="__codelineno-78-2" href="#__codelineno-78-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">start</span><span class="w"> </span><span class="err">起始位置，如果大于单词总数，则返回空串</span>
</span><span id="__span-78-3"><a id="__codelineno-78-3" name="__codelineno-78-3" href="#__codelineno-78-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">end</span><span class="w"> </span><span class="err">结束位置，如果大于单词总数，则返回起始位置之后全部，如果start</span><span class="w"> </span><span class="err">&gt;</span><span class="w"> </span><span class="err">end，什么都不返回</span>
</span><span id="__span-78-4"><a id="__codelineno-78-4" name="__codelineno-78-4" href="#__codelineno-78-4"></a>
</span><span id="__span-78-5"><a id="__codelineno-78-5" name="__codelineno-78-5" href="#__codelineno-78-5"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>test.cpp<span class="w"> </span>main.o<span class="w"> </span>hello.o<span class="w"> </span>hello.h<span class="w"> </span>main.cpp<span class="w"> </span>hello.cpp
</span><span id="__span-78-6"><a id="__codelineno-78-6" name="__codelineno-78-6" href="#__codelineno-78-6"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>wordlist<span class="w"> </span><span class="m">3</span>,6,<span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="words">words<a class="headerlink" href="#words" title="Permanent link">&para;</a></h4>
<p>返回文本中单词数</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-79-1"><a id="__codelineno-79-1" name="__codelineno-79-1" href="#__codelineno-79-1"></a><span class="k">$(</span><span class="nv">words</span> <span class="nv">text</span><span class="k">)</span>
</span><span id="__span-79-2"><a id="__codelineno-79-2" name="__codelineno-79-2" href="#__codelineno-79-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">text</span><span class="w"> </span><span class="err">需要处理的文本</span>
</span><span id="__span-79-3"><a id="__codelineno-79-3" name="__codelineno-79-3" href="#__codelineno-79-3"></a>
</span><span id="__span-79-4"><a id="__codelineno-79-4" name="__codelineno-79-4" href="#__codelineno-79-4"></a>
</span><span id="__span-79-5"><a id="__codelineno-79-5" name="__codelineno-79-5" href="#__codelineno-79-5"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>test.cpp<span class="w"> </span>main.o<span class="w"> </span>hello.o<span class="w"> </span>hello.h<span class="w"> </span>main.cpp<span class="w"> </span>hello.cpp
</span><span id="__span-79-6"><a id="__codelineno-79-6" name="__codelineno-79-6" href="#__codelineno-79-6"></a><span class="nv">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>words<span class="w"> </span><span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="firstword">firstword<a class="headerlink" href="#firstword" title="Permanent link">&para;</a></h4>
<p>返回第一个单词</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-80-1"><a id="__codelineno-80-1" name="__codelineno-80-1" href="#__codelineno-80-1"></a><span class="k">$(</span><span class="nv">firstword</span> <span class="nv">text</span><span class="k">)</span>
</span></code></pre></div>
<h4 id="lastword">lastword<a class="headerlink" href="#lastword" title="Permanent link">&para;</a></h4>
<p>返回最后一个单词</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-81-1"><a id="__codelineno-81-1" name="__codelineno-81-1" href="#__codelineno-81-1"></a><span class="k">$(</span><span class="nv">lastword</span> <span class="nv">text</span><span class="k">)</span>
</span></code></pre></div>
<h2 id="_44">文件名处理函数<a class="headerlink" href="#_44" title="Permanent link">&para;</a></h2>
<h4 id="dir">dir<a class="headerlink" href="#dir" title="Permanent link">&para;</a></h4>
<p>返回文件目录</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-82-1"><a id="__codelineno-82-1" name="__codelineno-82-1" href="#__codelineno-82-1"></a><span class="k">$(</span><span class="nv">dir</span> <span class="nv">files</span><span class="k">)</span>
</span><span id="__span-82-2"><a id="__codelineno-82-2" name="__codelineno-82-2" href="#__codelineno-82-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">files</span><span class="w"> </span><span class="err">需要返回目录的文件名，可以有多个，用空格隔开</span>
</span><span id="__span-82-3"><a id="__codelineno-82-3" name="__codelineno-82-3" href="#__codelineno-82-3"></a>
</span><span id="__span-82-4"><a id="__codelineno-82-4" name="__codelineno-82-4" href="#__codelineno-82-4"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>src/hello.cpp<span class="w"> </span>main.cpp
</span><span id="__span-82-5"><a id="__codelineno-82-5" name="__codelineno-82-5" href="#__codelineno-82-5"></a>
</span><span id="__span-82-6"><a id="__codelineno-82-6" name="__codelineno-82-6" href="#__codelineno-82-6"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>dir<span class="w"> </span><span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="notdir">notdir<a class="headerlink" href="#notdir" title="Permanent link">&para;</a></h4>
<p>返回除目录部分的文件名</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-83-1"><a id="__codelineno-83-1" name="__codelineno-83-1" href="#__codelineno-83-1"></a><span class="k">$(</span><span class="nv">notdir</span> <span class="nv">files</span><span class="k">)</span>
</span><span id="__span-83-2"><a id="__codelineno-83-2" name="__codelineno-83-2" href="#__codelineno-83-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">files</span><span class="w"> </span><span class="err">需要返回文件列表，可以有多个，用空格隔开</span>
</span><span id="__span-83-3"><a id="__codelineno-83-3" name="__codelineno-83-3" href="#__codelineno-83-3"></a>
</span><span id="__span-83-4"><a id="__codelineno-83-4" name="__codelineno-83-4" href="#__codelineno-83-4"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>src/hello.cpp<span class="w"> </span>main.cpp
</span><span id="__span-83-5"><a id="__codelineno-83-5" name="__codelineno-83-5" href="#__codelineno-83-5"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>notdir<span class="w"> </span><span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="suffix">suffix<a class="headerlink" href="#suffix" title="Permanent link">&para;</a></h4>
<p>返回文件后缀名，如果没有后缀返回空</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-84-1"><a id="__codelineno-84-1" name="__codelineno-84-1" href="#__codelineno-84-1"></a><span class="k">$(</span><span class="nv">suffix</span> <span class="nv">files</span><span class="k">)</span>
</span><span id="__span-84-2"><a id="__codelineno-84-2" name="__codelineno-84-2" href="#__codelineno-84-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">files</span><span class="w"> </span><span class="err">需要返回后缀的文件名，可以有多个，用空格隔开</span>
</span><span id="__span-84-3"><a id="__codelineno-84-3" name="__codelineno-84-3" href="#__codelineno-84-3"></a>
</span><span id="__span-84-4"><a id="__codelineno-84-4" name="__codelineno-84-4" href="#__codelineno-84-4"></a>
</span><span id="__span-84-5"><a id="__codelineno-84-5" name="__codelineno-84-5" href="#__codelineno-84-5"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>src/hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>hello.o<span class="w"> </span>hello.hpp<span class="w"> </span>hello
</span><span id="__span-84-6"><a id="__codelineno-84-6" name="__codelineno-84-6" href="#__codelineno-84-6"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>suffix<span class="w"> </span><span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="basename">basename<a class="headerlink" href="#basename" title="Permanent link">&para;</a></h4>
<p>返回文件名除后缀的部分</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-85-1"><a id="__codelineno-85-1" name="__codelineno-85-1" href="#__codelineno-85-1"></a><span class="k">$(</span><span class="nv">basename</span> <span class="nv">files</span><span class="k">)</span>
</span><span id="__span-85-2"><a id="__codelineno-85-2" name="__codelineno-85-2" href="#__codelineno-85-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">files</span><span class="w"> </span><span class="err">需要返回的文件名，可以有多个，用空格隔开</span>
</span><span id="__span-85-3"><a id="__codelineno-85-3" name="__codelineno-85-3" href="#__codelineno-85-3"></a>
</span><span id="__span-85-4"><a id="__codelineno-85-4" name="__codelineno-85-4" href="#__codelineno-85-4"></a>
</span><span id="__span-85-5"><a id="__codelineno-85-5" name="__codelineno-85-5" href="#__codelineno-85-5"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>src/hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>hello.o<span class="w"> </span>hello.hpp<span class="w"> </span>hello
</span><span id="__span-85-6"><a id="__codelineno-85-6" name="__codelineno-85-6" href="#__codelineno-85-6"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>basename<span class="w"> </span><span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="addsuffix">addsuffix<a class="headerlink" href="#addsuffix" title="Permanent link">&para;</a></h4>
<p>给文件名添加后缀</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-86-1"><a id="__codelineno-86-1" name="__codelineno-86-1" href="#__codelineno-86-1"></a><span class="k">$(</span><span class="nv">addsuffix</span> <span class="nv">suffix</span>,<span class="nv">files</span><span class="k">)</span>
</span><span id="__span-86-2"><a id="__codelineno-86-2" name="__codelineno-86-2" href="#__codelineno-86-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">suffix</span><span class="w"> </span><span class="err">需要添加的后缀</span>
</span><span id="__span-86-3"><a id="__codelineno-86-3" name="__codelineno-86-3" href="#__codelineno-86-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">files</span><span class="w"> </span><span class="err">需要添加后缀的文件名，可以有多个，用空格隔开</span>
</span><span id="__span-86-4"><a id="__codelineno-86-4" name="__codelineno-86-4" href="#__codelineno-86-4"></a>
</span><span id="__span-86-5"><a id="__codelineno-86-5" name="__codelineno-86-5" href="#__codelineno-86-5"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>src/hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>hello.o<span class="w"> </span>hello.hpp<span class="w"> </span>hello
</span><span id="__span-86-6"><a id="__codelineno-86-6" name="__codelineno-86-6" href="#__codelineno-86-6"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>addsuffix<span class="w"> </span>.exe,<span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="addprefix">addprefix<a class="headerlink" href="#addprefix" title="Permanent link">&para;</a></h4>
<p>给文件名添加前缀</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-87-1"><a id="__codelineno-87-1" name="__codelineno-87-1" href="#__codelineno-87-1"></a><span class="k">$(</span><span class="nv">addprefix</span> <span class="nv">prefix</span>,<span class="nv">files</span><span class="k">)</span>
</span><span id="__span-87-2"><a id="__codelineno-87-2" name="__codelineno-87-2" href="#__codelineno-87-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">prefix</span><span class="w"> </span><span class="err">需要添加的前缀</span>
</span><span id="__span-87-3"><a id="__codelineno-87-3" name="__codelineno-87-3" href="#__codelineno-87-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">files</span><span class="w"> </span><span class="err">需要添加前缀的文件名，可以有多个，用空格隔开</span>
</span><span id="__span-87-4"><a id="__codelineno-87-4" name="__codelineno-87-4" href="#__codelineno-87-4"></a>
</span><span id="__span-87-5"><a id="__codelineno-87-5" name="__codelineno-87-5" href="#__codelineno-87-5"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>src/hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>hello.o<span class="w"> </span>hello.hpp<span class="w"> </span>hello
</span><span id="__span-87-6"><a id="__codelineno-87-6" name="__codelineno-87-6" href="#__codelineno-87-6"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>addprefix<span class="w"> </span>make/,<span class="k">$(</span>files<span class="k">))</span>
</span></code></pre></div>
<h4 id="join">join<a class="headerlink" href="#join" title="Permanent link">&para;</a></h4>
<p>将两个列表中的内容一对一连接，如果两个列表内容数量不相等，则多出来的部分原样返回</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-88-1"><a id="__codelineno-88-1" name="__codelineno-88-1" href="#__codelineno-88-1"></a><span class="k">$(</span><span class="nv">join</span> <span class="nv">list1</span>,<span class="nv">list2</span><span class="k">)</span>
</span><span id="__span-88-2"><a id="__codelineno-88-2" name="__codelineno-88-2" href="#__codelineno-88-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">list1</span><span class="w"> </span><span class="err">第一个列表</span>
</span><span id="__span-88-3"><a id="__codelineno-88-3" name="__codelineno-88-3" href="#__codelineno-88-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">list2</span><span class="w"> </span><span class="err">需要连接的第二个列表</span>
</span><span id="__span-88-4"><a id="__codelineno-88-4" name="__codelineno-88-4" href="#__codelineno-88-4"></a>
</span><span id="__span-88-5"><a id="__codelineno-88-5" name="__codelineno-88-5" href="#__codelineno-88-5"></a>
</span><span id="__span-88-6"><a id="__codelineno-88-6" name="__codelineno-88-6" href="#__codelineno-88-6"></a><span class="nv">f1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello<span class="w"> </span>main<span class="w"> </span><span class="nb">test</span>
</span><span id="__span-88-7"><a id="__codelineno-88-7" name="__codelineno-88-7" href="#__codelineno-88-7"></a><span class="nv">f2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>.cpp<span class="w"> </span>.hpp
</span><span id="__span-88-8"><a id="__codelineno-88-8" name="__codelineno-88-8" href="#__codelineno-88-8"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>join<span class="w"> </span><span class="k">$(</span>f1<span class="k">)</span>,<span class="k">$(</span>f2<span class="k">))</span>
</span></code></pre></div>
<h4 id="wildcard">wildcard<a class="headerlink" href="#wildcard" title="Permanent link">&para;</a></h4>
<p>返回符合通配符的文件列表</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-89-1"><a id="__codelineno-89-1" name="__codelineno-89-1" href="#__codelineno-89-1"></a><span class="k">$(</span><span class="nv">wildcard</span> <span class="nv">pattern</span><span class="k">)</span>
</span><span id="__span-89-2"><a id="__codelineno-89-2" name="__codelineno-89-2" href="#__codelineno-89-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">pattern</span><span class="w"> </span><span class="err">通配符</span>
</span><span id="__span-89-3"><a id="__codelineno-89-3" name="__codelineno-89-3" href="#__codelineno-89-3"></a>
</span><span id="__span-89-4"><a id="__codelineno-89-4" name="__codelineno-89-4" href="#__codelineno-89-4"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>wildcard<span class="w"> </span>*.cpp<span class="k">)</span>
</span><span id="__span-89-5"><a id="__codelineno-89-5" name="__codelineno-89-5" href="#__codelineno-89-5"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>wildcard<span class="w"> </span>*<span class="k">)</span>
</span><span id="__span-89-6"><a id="__codelineno-89-6" name="__codelineno-89-6" href="#__codelineno-89-6"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>wildcard<span class="w"> </span>src/*.cpp<span class="k">)</span>
</span></code></pre></div>
<h4 id="realpath">realpath<a class="headerlink" href="#realpath" title="Permanent link">&para;</a></h4>
<p>返回文件的绝对路径</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-90-1"><a id="__codelineno-90-1" name="__codelineno-90-1" href="#__codelineno-90-1"></a><span class="k">$(</span><span class="nv">realpath</span> <span class="nv">files</span><span class="k">)</span>
</span><span id="__span-90-2"><a id="__codelineno-90-2" name="__codelineno-90-2" href="#__codelineno-90-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">files</span><span class="w"> </span><span class="err">需要返回绝对路径的文件，可以有多个，用空格隔开</span>
</span><span id="__span-90-3"><a id="__codelineno-90-3" name="__codelineno-90-3" href="#__codelineno-90-3"></a>
</span><span id="__span-90-4"><a id="__codelineno-90-4" name="__codelineno-90-4" href="#__codelineno-90-4"></a><span class="nv">f3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>wildcard<span class="w"> </span>src/*<span class="k">)</span>
</span><span id="__span-90-5"><a id="__codelineno-90-5" name="__codelineno-90-5" href="#__codelineno-90-5"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>realpath<span class="w"> </span><span class="k">$(</span>f3<span class="k">))</span>
</span></code></pre></div>
<h4 id="abspath">abspath<a class="headerlink" href="#abspath" title="Permanent link">&para;</a></h4>
<p>返回绝对路径，用法同realpath，如果一个文件名不存在，realpath不会返回内容，abspath则会返回一个当前文件夹一下的绝对路径</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-91-1"><a id="__codelineno-91-1" name="__codelineno-91-1" href="#__codelineno-91-1"></a><span class="k">$(</span><span class="nv">abspath</span> <span class="nv">files</span><span class="k">)</span>
</span></code></pre></div>
<h2 id="_45">条件函数<a class="headerlink" href="#_45" title="Permanent link">&para;</a></h2>
<h4 id="if">if<a class="headerlink" href="#if" title="Permanent link">&para;</a></h4>
<p>条伯判断，如果条件展开不是空串，则反回真的部分，否则返回假的部分</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-92-1"><a id="__codelineno-92-1" name="__codelineno-92-1" href="#__codelineno-92-1"></a><span class="k">$(</span><span class="nv">if</span> <span class="nv">condition</span>,<span class="nv">then-part</span>[,<span class="nv">else-part</span>]<span class="k">)</span>
</span><span id="__span-92-2"><a id="__codelineno-92-2" name="__codelineno-92-2" href="#__codelineno-92-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">condition</span><span class="w"> </span><span class="err">条件部分</span>
</span><span id="__span-92-3"><a id="__codelineno-92-3" name="__codelineno-92-3" href="#__codelineno-92-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">then-part</span><span class="w"> </span><span class="err">条件为真时执行的部分</span>
</span><span id="__span-92-4"><a id="__codelineno-92-4" name="__codelineno-92-4" href="#__codelineno-92-4"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">else-part</span><span class="w"> </span><span class="err">条件为假时执行的部分，如果省略则为假时返回空串</span>
</span><span id="__span-92-5"><a id="__codelineno-92-5" name="__codelineno-92-5" href="#__codelineno-92-5"></a>
</span><span id="__span-92-6"><a id="__codelineno-92-6" name="__codelineno-92-6" href="#__codelineno-92-6"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>src/hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>hello.o<span class="w"> </span>hello.hpp<span class="w"> </span>hello
</span><span id="__span-92-7"><a id="__codelineno-92-7" name="__codelineno-92-7" href="#__codelineno-92-7"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(if</span><span class="w"> </span><span class="k">$(</span>files<span class="k">)</span>,有文件,没有文件<span class="k">)</span>
</span></code></pre></div>
<h4 id="or">or<a class="headerlink" href="#or" title="Permanent link">&para;</a></h4>
<p>返回条件中第一个不为空的部分</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-93-1"><a id="__codelineno-93-1" name="__codelineno-93-1" href="#__codelineno-93-1"></a><span class="k">$(</span><span class="nv">or</span> <span class="nv">condition1</span>[,<span class="nv">condition2</span>[,<span class="nv">condition3</span>…]]<span class="k">)</span>
</span><span id="__span-93-2"><a id="__codelineno-93-2" name="__codelineno-93-2" href="#__codelineno-93-2"></a>
</span><span id="__span-93-3"><a id="__codelineno-93-3" name="__codelineno-93-3" href="#__codelineno-93-3"></a><span class="nv">f1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>
</span><span id="__span-93-4"><a id="__codelineno-93-4" name="__codelineno-93-4" href="#__codelineno-93-4"></a><span class="nv">f2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>
</span><span id="__span-93-5"><a id="__codelineno-93-5" name="__codelineno-93-5" href="#__codelineno-93-5"></a><span class="nv">f3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello.cpp
</span><span id="__span-93-6"><a id="__codelineno-93-6" name="__codelineno-93-6" href="#__codelineno-93-6"></a><span class="nv">f4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>main.cpp
</span><span id="__span-93-7"><a id="__codelineno-93-7" name="__codelineno-93-7" href="#__codelineno-93-7"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>or<span class="w"> </span><span class="k">$(</span>f1<span class="k">)</span>,<span class="k">$(</span>f2<span class="k">)</span>,<span class="k">$(</span>f3<span class="k">)</span>,<span class="k">$(</span>f4<span class="k">))</span>
</span></code></pre></div>
<h4 id="and">and<a class="headerlink" href="#and" title="Permanent link">&para;</a></h4>
<p>如果条件中有一个为空串，则返回空，如果全都不为空，则返回最后一个条件</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-94-1"><a id="__codelineno-94-1" name="__codelineno-94-1" href="#__codelineno-94-1"></a><span class="k">$(</span><span class="nv">and</span> <span class="nv">condition1</span>[,<span class="nv">condition2</span>[,<span class="nv">condition3</span>…]]<span class="k">)</span>
</span><span id="__span-94-2"><a id="__codelineno-94-2" name="__codelineno-94-2" href="#__codelineno-94-2"></a>
</span><span id="__span-94-3"><a id="__codelineno-94-3" name="__codelineno-94-3" href="#__codelineno-94-3"></a><span class="nv">f1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">12</span>
</span><span id="__span-94-4"><a id="__codelineno-94-4" name="__codelineno-94-4" href="#__codelineno-94-4"></a><span class="nv">f2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">34</span>
</span><span id="__span-94-5"><a id="__codelineno-94-5" name="__codelineno-94-5" href="#__codelineno-94-5"></a><span class="nv">f3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hello.cpp
</span><span id="__span-94-6"><a id="__codelineno-94-6" name="__codelineno-94-6" href="#__codelineno-94-6"></a><span class="nv">f4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>main.cpp
</span><span id="__span-94-7"><a id="__codelineno-94-7" name="__codelineno-94-7" href="#__codelineno-94-7"></a><span class="nv">files2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>and<span class="w"> </span><span class="k">$(</span>f1<span class="k">)</span>,<span class="k">$(</span>f2<span class="k">)</span>,<span class="k">$(</span>f3<span class="k">)</span>,<span class="k">$(</span>f4<span class="k">))</span>
</span></code></pre></div>
<h4 id="intcmp">intcmp<a class="headerlink" href="#intcmp" title="Permanent link">&para;</a></h4>
<p>比较两个整数大小，并返回对应操作结果（GNU make 4.4以上版本）</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-95-1"><a id="__codelineno-95-1" name="__codelineno-95-1" href="#__codelineno-95-1"></a><span class="k">$(</span><span class="nv">intcmp</span> <span class="nv">lhs</span>,<span class="nv">rhs</span>[,<span class="nv">lt-part</span>[,<span class="nv">eq-part</span>[,<span class="nv">gt-part</span>]]]<span class="k">)</span><span class="w"> </span>
</span><span id="__span-95-2"><a id="__codelineno-95-2" name="__codelineno-95-2" href="#__codelineno-95-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">lhs</span><span class="w"> </span><span class="err">第一个数</span>
</span><span id="__span-95-3"><a id="__codelineno-95-3" name="__codelineno-95-3" href="#__codelineno-95-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">rhs</span><span class="w"> </span><span class="err">第二个数</span>
</span><span id="__span-95-4"><a id="__codelineno-95-4" name="__codelineno-95-4" href="#__codelineno-95-4"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">lt-part</span><span class="w">  </span><span class="err">lhs</span><span class="w"> </span><span class="err">&lt;</span><span class="w"> </span><span class="err">rhs时执行</span>
</span><span id="__span-95-5"><a id="__codelineno-95-5" name="__codelineno-95-5" href="#__codelineno-95-5"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">eq-part</span><span class="w">  </span><span class="nv">lhs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>rhs时执行
</span><span id="__span-95-6"><a id="__codelineno-95-6" name="__codelineno-95-6" href="#__codelineno-95-6"></a><span class="w">        </span>---<span class="w"> </span>gt-part<span class="w">  </span>lhs<span class="w"> </span>&gt;<span class="w"> </span>rhs时执行
</span><span id="__span-95-7"><a id="__codelineno-95-7" name="__codelineno-95-7" href="#__codelineno-95-7"></a><span class="w">        </span>---<span class="w"> </span>如果只提供前两个参数，则lhs<span class="w"> </span><span class="o">==</span><span class="w"> </span>rhs时返回数值，否则返回空串
</span><span id="__span-95-8"><a id="__codelineno-95-8" name="__codelineno-95-8" href="#__codelineno-95-8"></a><span class="w">            </span>参数为lhs,rhs,lt-part时，当lhs<span class="w"> </span>&lt;<span class="w"> </span>rhs时返回lt-part结果，否则返回空
</span><span id="__span-95-9"><a id="__codelineno-95-9" name="__codelineno-95-9" href="#__codelineno-95-9"></a><span class="w">            </span>参数为lhs,rhs,lt-part,eq-part，lhs<span class="w"> </span>&lt;<span class="w"> </span>rhs返回lt-part结果，否则都返回eq-part结果
</span><span id="__span-95-10"><a id="__codelineno-95-10" name="__codelineno-95-10" href="#__codelineno-95-10"></a><span class="w">            </span>参数全时，lhs<span class="w"> </span>&lt;<span class="w"> </span>rhs返回lt-part，lhs<span class="w"> </span><span class="o">==</span><span class="w"> </span>rhs返回eq-part,<span class="w"> </span>lhs<span class="w"> </span>&gt;<span class="w"> </span>rhs返回gt-part
</span><span id="__span-95-11"><a id="__codelineno-95-11" name="__codelineno-95-11" href="#__codelineno-95-11"></a>
</span><span id="__span-95-12"><a id="__codelineno-95-12" name="__codelineno-95-12" href="#__codelineno-95-12"></a>
</span><span id="__span-95-13"><a id="__codelineno-95-13" name="__codelineno-95-13" href="#__codelineno-95-13"></a>
</span><span id="__span-95-14"><a id="__codelineno-95-14" name="__codelineno-95-14" href="#__codelineno-95-14"></a><span class="err">@echo</span><span class="w"> </span><span class="k">$(</span><span class="nv">intcmp</span> <span class="nv">2</span>,<span class="nv">2</span>,<span class="nv">-1</span>,<span class="nv">0</span>,<span class="nv">1</span><span class="k">)</span>
</span></code></pre></div>
<h2 id="file">file<a class="headerlink" href="#file" title="Permanent link">&para;</a></h2>
<p>读写文件</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-96-1"><a id="__codelineno-96-1" name="__codelineno-96-1" href="#__codelineno-96-1"></a><span class="k">$(</span><span class="nv">file</span> <span class="nv">op</span> <span class="nv">filename</span>[,<span class="nv">text</span>]<span class="k">)</span>
</span><span id="__span-96-2"><a id="__codelineno-96-2" name="__codelineno-96-2" href="#__codelineno-96-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">op</span><span class="w"> </span><span class="err">操作</span>
</span><span id="__span-96-3"><a id="__codelineno-96-3" name="__codelineno-96-3" href="#__codelineno-96-3"></a><span class="w">                </span><span class="err">&gt;</span><span class="w"> </span><span class="err">覆盖</span>
</span><span id="__span-96-4"><a id="__codelineno-96-4" name="__codelineno-96-4" href="#__codelineno-96-4"></a><span class="w">                </span><span class="err">&gt;&gt;</span><span class="w"> </span><span class="err">追加</span>
</span><span id="__span-96-5"><a id="__codelineno-96-5" name="__codelineno-96-5" href="#__codelineno-96-5"></a><span class="w">                </span><span class="err">&lt;</span><span class="w"> </span><span class="err">读</span>
</span><span id="__span-96-6"><a id="__codelineno-96-6" name="__codelineno-96-6" href="#__codelineno-96-6"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">filename</span><span class="w"> </span><span class="err">需要操作的文件名</span>
</span><span id="__span-96-7"><a id="__codelineno-96-7" name="__codelineno-96-7" href="#__codelineno-96-7"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">text</span><span class="w"> </span><span class="err">写入的文本内容，读取是不需要这个参数</span>
</span><span id="__span-96-8"><a id="__codelineno-96-8" name="__codelineno-96-8" href="#__codelineno-96-8"></a>
</span><span id="__span-96-9"><a id="__codelineno-96-9" name="__codelineno-96-9" href="#__codelineno-96-9"></a>
</span><span id="__span-96-10"><a id="__codelineno-96-10" name="__codelineno-96-10" href="#__codelineno-96-10"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>src/hello.cpp<span class="w"> </span>main.cpp<span class="w"> </span>hello.o<span class="w"> </span>hello.hpp<span class="w"> </span>hello
</span><span id="__span-96-11"><a id="__codelineno-96-11" name="__codelineno-96-11" href="#__codelineno-96-11"></a><span class="nv">write</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>file<span class="w"> </span>&gt;<span class="w"> </span>makewrite.txt,<span class="k">$(</span>files<span class="k">))</span>
</span><span id="__span-96-12"><a id="__codelineno-96-12" name="__codelineno-96-12" href="#__codelineno-96-12"></a><span class="nv">read</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>file<span class="w"> </span>&lt;<span class="w"> </span>makewrite.txt<span class="k">)</span>
</span></code></pre></div>
<h2 id="foreach">foreach<a class="headerlink" href="#foreach" title="Permanent link">&para;</a></h2>
<p>对一列用空格隔开的字符序列中每一项进行处理，并返回处理后的列表</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-97-1"><a id="__codelineno-97-1" name="__codelineno-97-1" href="#__codelineno-97-1"></a><span class="k">$(</span><span class="nv">foreach</span> <span class="nv">each</span>,<span class="nv">list</span>,<span class="nv">process</span><span class="k">)</span>
</span><span id="__span-97-2"><a id="__codelineno-97-2" name="__codelineno-97-2" href="#__codelineno-97-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">each</span><span class="w"> </span><span class="err">list中的每一项</span>
</span><span id="__span-97-3"><a id="__codelineno-97-3" name="__codelineno-97-3" href="#__codelineno-97-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">list</span><span class="w"> </span><span class="err">需要处理的字符串序列，用空格隔开</span>
</span><span id="__span-97-4"><a id="__codelineno-97-4" name="__codelineno-97-4" href="#__codelineno-97-4"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">process</span><span class="w"> </span><span class="err">需要对每一项进行的处理</span>
</span><span id="__span-97-5"><a id="__codelineno-97-5" name="__codelineno-97-5" href="#__codelineno-97-5"></a>
</span><span id="__span-97-6"><a id="__codelineno-97-6" name="__codelineno-97-6" href="#__codelineno-97-6"></a><span class="nv">list</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="m">2</span><span class="w"> </span><span class="m">3</span><span class="w"> </span><span class="m">4</span><span class="w"> </span><span class="m">5</span>
</span><span id="__span-97-7"><a id="__codelineno-97-7" name="__codelineno-97-7" href="#__codelineno-97-7"></a><span class="nv">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>foreach<span class="w"> </span>each,<span class="k">$(</span>list<span class="k">)</span>,<span class="k">$(</span>addprefix<span class="w"> </span>cpp,<span class="k">$(</span>addsuffix<span class="w"> </span>.cpp,<span class="k">$(</span>each<span class="k">))))</span>
</span></code></pre></div>
<p>作用类似C/C++中的循环</p>
<div class="language-c++ highlight"><pre><span></span><code><span id="__span-98-1"><a id="__codelineno-98-1" name="__codelineno-98-1" href="#__codelineno-98-1"></a><span class="kt">int</span><span class="w"> </span><span class="n">list</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">};</span>
</span><span id="__span-98-2"><a id="__codelineno-98-2" name="__codelineno-98-2" href="#__codelineno-98-2"></a><span class="kt">int</span><span class="w"> </span><span class="n">result</span><span class="p">[</span><span class="mi">5</span><span class="p">];</span>
</span><span id="__span-98-3"><a id="__codelineno-98-3" name="__codelineno-98-3" href="#__codelineno-98-3"></a><span class="kt">int</span><span class="w"> </span><span class="n">each</span><span class="p">;</span>
</span><span id="__span-98-4"><a id="__codelineno-98-4" name="__codelineno-98-4" href="#__codelineno-98-4"></a><span class="k">for</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
</span><span id="__span-98-5"><a id="__codelineno-98-5" name="__codelineno-98-5" href="#__codelineno-98-5"></a><span class="p">{</span>
</span><span id="__span-98-6"><a id="__codelineno-98-6" name="__codelineno-98-6" href="#__codelineno-98-6"></a><span class="w">    </span><span class="n">each</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">list</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
</span><span id="__span-98-7"><a id="__codelineno-98-7" name="__codelineno-98-7" href="#__codelineno-98-7"></a><span class="w">    </span><span class="n">result</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">process</span><span class="p">(</span><span class="n">each</span><span class="p">);</span>
</span><span id="__span-98-8"><a id="__codelineno-98-8" name="__codelineno-98-8" href="#__codelineno-98-8"></a><span class="p">}</span>
</span><span id="__span-98-9"><a id="__codelineno-98-9" name="__codelineno-98-9" href="#__codelineno-98-9"></a><span class="c1">// 此时result即为返回结果</span>
</span></code></pre></div>
<h2 id="call">call<a class="headerlink" href="#call" title="Permanent link">&para;</a></h2>
<p>将一些复杂的表达式写成一个变量，用call可以像调用函数一样进行调用。类似于编程语言中的自定义函数。在函数中可以用$(n)来访问第n个参数</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-99-1"><a id="__codelineno-99-1" name="__codelineno-99-1" href="#__codelineno-99-1"></a><span class="k">$(</span><span class="nv">call</span> <span class="nv">funcname</span>,<span class="nv">param1</span>,<span class="nv">param2</span>,…<span class="k">)</span>
</span><span id="__span-99-2"><a id="__codelineno-99-2" name="__codelineno-99-2" href="#__codelineno-99-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">funcname</span><span class="w"> </span><span class="err">自定义函数（变量名）</span>
</span><span id="__span-99-3"><a id="__codelineno-99-3" name="__codelineno-99-3" href="#__codelineno-99-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">参数至少一个，可以有多个，用逗号隔开</span>
</span><span id="__span-99-4"><a id="__codelineno-99-4" name="__codelineno-99-4" href="#__codelineno-99-4"></a>
</span><span id="__span-99-5"><a id="__codelineno-99-5" name="__codelineno-99-5" href="#__codelineno-99-5"></a><span class="nv">dirof</span><span class="w"> </span><span class="o">=</span><span class="w">  </span><span class="k">$(</span>dir<span class="w"> </span><span class="k">$(</span>realpath<span class="w"> </span><span class="k">$(</span><span class="m">1</span><span class="k">)))</span><span class="w"> </span><span class="k">$(</span>dir<span class="w"> </span><span class="k">$(</span>realpath<span class="w"> </span><span class="k">$(</span><span class="m">2</span><span class="k">)))</span>
</span><span id="__span-99-6"><a id="__codelineno-99-6" name="__codelineno-99-6" href="#__codelineno-99-6"></a><span class="nv">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>call<span class="w"> </span>dirof,main.cpp,src/hello.cpp<span class="k">)</span>
</span></code></pre></div>
<h2 id="value">value<a class="headerlink" href="#value" title="Permanent link">&para;</a></h2>
<p>对于不是立即展开的变量，可以查看变量的原始定义；对于立即展开的变量，直接返回变量值</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-100-1"><a id="__codelineno-100-1" name="__codelineno-100-1" href="#__codelineno-100-1"></a><span class="k">$(</span><span class="nv">value</span> <span class="nv">variable</span><span class="k">)</span>
</span><span id="__span-100-2"><a id="__codelineno-100-2" name="__codelineno-100-2" href="#__codelineno-100-2"></a>
</span><span id="__span-100-3"><a id="__codelineno-100-3" name="__codelineno-100-3" href="#__codelineno-100-3"></a><span class="nv">var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>value<span class="w"> </span><span class="k">function</span><span class="w"> </span><span class="nb">test</span>
</span><span id="__span-100-4"><a id="__codelineno-100-4" name="__codelineno-100-4" href="#__codelineno-100-4"></a><span class="nv">var2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>var<span class="k">)</span>
</span><span id="__span-100-5"><a id="__codelineno-100-5" name="__codelineno-100-5" href="#__codelineno-100-5"></a><span class="nv">var3</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$(</span>var<span class="k">)</span>
</span><span id="__span-100-6"><a id="__codelineno-100-6" name="__codelineno-100-6" href="#__codelineno-100-6"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-100-7"><a id="__codelineno-100-7" name="__codelineno-100-7" href="#__codelineno-100-7"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>value<span class="w"> </span>var2<span class="k">)</span>
</span><span id="__span-100-8"><a id="__codelineno-100-8" name="__codelineno-100-8" href="#__codelineno-100-8"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>value<span class="w"> </span>var3<span class="k">)</span>
</span></code></pre></div>
<h2 id="_46"><a class="headerlink" href="#_46" title="Permanent link">&para;</a></h2>
<p>查看一个变量定义来源</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-101-1"><a id="__codelineno-101-1" name="__codelineno-101-1" href="#__codelineno-101-1"></a><span class="k">$(</span><span class="nv">origin</span> <span class="nv">variable</span><span class="k">)</span>
</span><span id="__span-101-2"><a id="__codelineno-101-2" name="__codelineno-101-2" href="#__codelineno-101-2"></a>
</span><span id="__span-101-3"><a id="__codelineno-101-3" name="__codelineno-101-3" href="#__codelineno-101-3"></a>
</span><span id="__span-101-4"><a id="__codelineno-101-4" name="__codelineno-101-4" href="#__codelineno-101-4"></a><span class="nv">var2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>origin<span class="w"> </span><span class="k">function</span><span class="w"> </span>
</span><span id="__span-101-5"><a id="__codelineno-101-5" name="__codelineno-101-5" href="#__codelineno-101-5"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-101-6"><a id="__codelineno-101-6" name="__codelineno-101-6" href="#__codelineno-101-6"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>origin<span class="w"> </span>var1<span class="k">)</span><span class="w">    </span><span class="c1"># undefined 未定义</span>
</span><span id="__span-101-7"><a id="__codelineno-101-7" name="__codelineno-101-7" href="#__codelineno-101-7"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>origin<span class="w"> </span>CC<span class="k">)</span><span class="w">        </span><span class="c1"># default 默认变量</span>
</span><span id="__span-101-8"><a id="__codelineno-101-8" name="__codelineno-101-8" href="#__codelineno-101-8"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>origin<span class="w"> </span>JAVA_HOME<span class="k">)</span><span class="w"> </span><span class="c1"># environment 环境变量</span>
</span><span id="__span-101-9"><a id="__codelineno-101-9" name="__codelineno-101-9" href="#__codelineno-101-9"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>origin<span class="w"> </span>var2<span class="k">)</span><span class="w">    </span><span class="c1"># file 在Makefile文件中定义的变量</span>
</span><span id="__span-101-10"><a id="__codelineno-101-10" name="__codelineno-101-10" href="#__codelineno-101-10"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>origin<span class="w"> </span>@<span class="k">)</span><span class="w">        </span><span class="c1"># automatic 自动变量</span>
</span></code></pre></div>
<h2 id="flavor">flavor<a class="headerlink" href="#flavor" title="Permanent link">&para;</a></h2>
<p>查看一个变量的赋值方式</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-102-1"><a id="__codelineno-102-1" name="__codelineno-102-1" href="#__codelineno-102-1"></a><span class="k">$(</span><span class="nv">flavor</span> <span class="nv">variable</span><span class="k">)</span>
</span><span id="__span-102-2"><a id="__codelineno-102-2" name="__codelineno-102-2" href="#__codelineno-102-2"></a>
</span><span id="__span-102-3"><a id="__codelineno-102-3" name="__codelineno-102-3" href="#__codelineno-102-3"></a><span class="nv">var2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>flavor<span class="w"> </span><span class="k">function</span>
</span><span id="__span-102-4"><a id="__codelineno-102-4" name="__codelineno-102-4" href="#__codelineno-102-4"></a><span class="nv">var3</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span>flavor<span class="w"> </span>funciton
</span><span id="__span-102-5"><a id="__codelineno-102-5" name="__codelineno-102-5" href="#__codelineno-102-5"></a><span class="nf">all</span><span class="o">:</span>
</span><span id="__span-102-6"><a id="__codelineno-102-6" name="__codelineno-102-6" href="#__codelineno-102-6"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>flavor<span class="w"> </span>var1<span class="k">)</span><span class="w">    </span><span class="c1"># undefined 未定义</span>
</span><span id="__span-102-7"><a id="__codelineno-102-7" name="__codelineno-102-7" href="#__codelineno-102-7"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>flavor<span class="w"> </span>var2<span class="k">)</span><span class="w">    </span><span class="c1"># recursive 递归展开赋值</span>
</span><span id="__span-102-8"><a id="__codelineno-102-8" name="__codelineno-102-8" href="#__codelineno-102-8"></a><span class="w">    </span>@echo<span class="w"> </span><span class="k">$(</span>flavor<span class="w"> </span>var3<span class="k">)</span><span class="w">    </span><span class="c1"># simple 简单赋值</span>
</span></code></pre></div>
<h2 id="eval">eval<a class="headerlink" href="#eval" title="Permanent link">&para;</a></h2>
<p>可以将一段文本生成Makefile的内容</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-103-1"><a id="__codelineno-103-1" name="__codelineno-103-1" href="#__codelineno-103-1"></a><span class="k">$(</span><span class="nv">eval</span> <span class="nv">text</span><span class="k">)</span>
</span><span id="__span-103-2"><a id="__codelineno-103-2" name="__codelineno-103-2" href="#__codelineno-103-2"></a>
</span><span id="__span-103-3"><a id="__codelineno-103-3" name="__codelineno-103-3" href="#__codelineno-103-3"></a><span class="cp">define eval_target = </span>
</span><span id="__span-103-4"><a id="__codelineno-103-4" name="__codelineno-103-4" href="#__codelineno-103-4"></a><span class="nf">eval</span><span class="o">:</span>
</span><span id="__span-103-5"><a id="__codelineno-103-5" name="__codelineno-103-5" href="#__codelineno-103-5"></a><span class="w">    </span>@echo<span class="w"> </span>Target<span class="w"> </span>Eval<span class="w"> </span>Test
</span><span id="__span-103-6"><a id="__codelineno-103-6" name="__codelineno-103-6" href="#__codelineno-103-6"></a><span class="cp">endef</span>
</span><span id="__span-103-7"><a id="__codelineno-103-7" name="__codelineno-103-7" href="#__codelineno-103-7"></a>
</span><span id="__span-103-8"><a id="__codelineno-103-8" name="__codelineno-103-8" href="#__codelineno-103-8"></a><span class="k">$(</span><span class="nv">eval</span> <span class="k">$(</span><span class="nv">eval_target</span><span class="k">))</span>
</span></code></pre></div>
<p>以上，运行make时将会执行eval目标</p>
<h2 id="shell_2">shell<a class="headerlink" href="#shell_2" title="Permanent link">&para;</a></h2>
<p>用于执行Shell命令</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-104-1"><a id="__codelineno-104-1" name="__codelineno-104-1" href="#__codelineno-104-1"></a><span class="nv">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span>shell<span class="w"> </span>ls<span class="w"> </span>*.cpp<span class="k">)</span>
</span><span id="__span-104-2"><a id="__codelineno-104-2" name="__codelineno-104-2" href="#__codelineno-104-2"></a><span class="k">$(</span><span class="nv">shell</span> <span class="nv">echo</span> <span class="nv">This</span> <span class="nv">is</span> <span class="nv">from</span> <span class="nv">shell</span> <span class="nv">function</span><span class="k">)</span>
</span></code></pre></div>
<h2 id="let">let<a class="headerlink" href="#let" title="Permanent link">&para;</a></h2>
<p>将一个字符串序列中的项拆开放入多个变量中，并对各个变量进行操作（GNU make 4.4以上版本）</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-105-1"><a id="__codelineno-105-1" name="__codelineno-105-1" href="#__codelineno-105-1"></a><span class="k">$(</span><span class="nv">let</span> <span class="nv">var1</span> [<span class="nv">var2</span> <span class="nv">...</span>],[<span class="nv">list</span>],<span class="nv">proc</span><span class="k">)</span>
</span><span id="__span-105-2"><a id="__codelineno-105-2" name="__codelineno-105-2" href="#__codelineno-105-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">var</span><span class="w"> </span><span class="err">变量，可以有多个，用空格隔开</span>
</span><span id="__span-105-3"><a id="__codelineno-105-3" name="__codelineno-105-3" href="#__codelineno-105-3"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">list</span><span class="w"> </span><span class="err">待处理字符串，各项之间空格隔开</span>
</span><span id="__span-105-4"><a id="__codelineno-105-4" name="__codelineno-105-4" href="#__codelineno-105-4"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">proc</span><span class="w"> </span><span class="err">对变量进行的操作，结果为let的返回值</span>
</span><span id="__span-105-5"><a id="__codelineno-105-5" name="__codelineno-105-5" href="#__codelineno-105-5"></a><span class="w">            </span><span class="err">将list中的值依次一项一项放到var中，如果var的个数多于list项数，那多出来的var是空串。如果</span>
</span><span id="__span-105-6"><a id="__codelineno-105-6" name="__codelineno-105-6" href="#__codelineno-105-6"></a><span class="w">            </span><span class="err">var的个数小于list项数，则先依次把前而的项放入var中，剩下的list所有项都放入最后一个var中</span>
</span><span id="__span-105-7"><a id="__codelineno-105-7" name="__codelineno-105-7" href="#__codelineno-105-7"></a>
</span><span id="__span-105-8"><a id="__codelineno-105-8" name="__codelineno-105-8" href="#__codelineno-105-8"></a>
</span><span id="__span-105-9"><a id="__codelineno-105-9" name="__codelineno-105-9" href="#__codelineno-105-9"></a><span class="nv">list</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>a<span class="w"> </span>b<span class="w"> </span>c<span class="w"> </span>d
</span><span id="__span-105-10"><a id="__codelineno-105-10" name="__codelineno-105-10" href="#__codelineno-105-10"></a><span class="nv">letfirst</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span><span class="nb">let</span><span class="w"> </span>first<span class="w"> </span>second<span class="w"> </span>rest,<span class="k">$(</span>list<span class="k">)</span>,<span class="k">$(</span>first<span class="k">))</span>
</span><span id="__span-105-11"><a id="__codelineno-105-11" name="__codelineno-105-11" href="#__codelineno-105-11"></a><span class="nv">letrest</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span><span class="nb">let</span><span class="w"> </span>first<span class="w"> </span>second<span class="w"> </span>rest,<span class="k">$(</span>list<span class="k">)</span>,<span class="k">$(</span>rest<span class="k">))</span>
</span><span id="__span-105-12"><a id="__codelineno-105-12" name="__codelineno-105-12" href="#__codelineno-105-12"></a>
</span><span id="__span-105-13"><a id="__codelineno-105-13" name="__codelineno-105-13" href="#__codelineno-105-13"></a>
</span><span id="__span-105-14"><a id="__codelineno-105-14" name="__codelineno-105-14" href="#__codelineno-105-14"></a><span class="c"># 结合call可以对所有项进行递归处理</span>
</span><span id="__span-105-15"><a id="__codelineno-105-15" name="__codelineno-105-15" href="#__codelineno-105-15"></a><span class="nv">reverse</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">$(</span><span class="nb">let</span><span class="w"> </span>first<span class="w"> </span>rest,<span class="k">$(</span><span class="m">1</span><span class="k">)</span>,<span class="k">$(if</span><span class="w"> </span><span class="k">$(</span>rest<span class="k">)</span>,<span class="k">$(</span>call<span class="w"> </span>reverse,<span class="k">$(</span>rest<span class="k">))</span><span class="w"> </span><span class="k">)$(</span>first<span class="k">))</span>
</span><span id="__span-105-16"><a id="__codelineno-105-16" name="__codelineno-105-16" href="#__codelineno-105-16"></a><span class="nf">all</span><span class="o">:</span><span class="w"> </span>; @<span class="n">echo</span> <span class="k">$(</span><span class="nv">call</span> <span class="nv">reverse</span>,<span class="nv">d</span> <span class="nv">c</span> <span class="nv">b</span> <span class="nv">a</span><span class="k">)</span>
</span></code></pre></div>
<h2 id="_47">信息提示函数<a class="headerlink" href="#_47" title="Permanent link">&para;</a></h2>
<h4 id="error">error<a class="headerlink" href="#error" title="Permanent link">&para;</a></h4>
<p>提示错误信息并终止make执行</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-106-1"><a id="__codelineno-106-1" name="__codelineno-106-1" href="#__codelineno-106-1"></a><span class="k">$(</span><span class="nv">error</span> <span class="nv">text</span><span class="k">)</span>
</span><span id="__span-106-2"><a id="__codelineno-106-2" name="__codelineno-106-2" href="#__codelineno-106-2"></a><span class="w">        </span><span class="err">---</span><span class="w"> </span><span class="err">text</span><span class="w"> </span><span class="err">提示信息</span>
</span><span id="__span-106-3"><a id="__codelineno-106-3" name="__codelineno-106-3" href="#__codelineno-106-3"></a>
</span><span id="__span-106-4"><a id="__codelineno-106-4" name="__codelineno-106-4" href="#__codelineno-106-4"></a><span class="nv">EXIT_STATUS</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>-1
</span><span id="__span-106-5"><a id="__codelineno-106-5" name="__codelineno-106-5" href="#__codelineno-106-5"></a><span class="cp">ifneq (0, $(EXIT_STATUS))</span>
</span><span id="__span-106-6"><a id="__codelineno-106-6" name="__codelineno-106-6" href="#__codelineno-106-6"></a><span class="w">    </span><span class="k">$(</span>error<span class="w"> </span>An<span class="w"> </span>error<span class="w"> </span>occured!<span class="w"> </span>make<span class="w"> </span>stopped!<span class="k">)</span>
</span><span id="__span-106-7"><a id="__codelineno-106-7" name="__codelineno-106-7" href="#__codelineno-106-7"></a><span class="cp">endif</span>
</span></code></pre></div>
<h4 id="warning">warning<a class="headerlink" href="#warning" title="Permanent link">&para;</a></h4>
<p>提示警告信息，make不会终止</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-107-1"><a id="__codelineno-107-1" name="__codelineno-107-1" href="#__codelineno-107-1"></a><span class="k">$(</span><span class="nv">warning</span> <span class="nv">text</span><span class="k">)</span>
</span><span id="__span-107-2"><a id="__codelineno-107-2" name="__codelineno-107-2" href="#__codelineno-107-2"></a>
</span><span id="__span-107-3"><a id="__codelineno-107-3" name="__codelineno-107-3" href="#__codelineno-107-3"></a><span class="cp">ifneq (0, $(EXIT_STATUS))</span>
</span><span id="__span-107-4"><a id="__codelineno-107-4" name="__codelineno-107-4" href="#__codelineno-107-4"></a><span class="w">    </span><span class="k">$(</span><span class="nv">warning</span> <span class="nv">This</span> <span class="nv">is</span> <span class="nv">a</span> <span class="nv">warning</span> <span class="nv">message</span><span class="k">)</span>
</span><span id="__span-107-5"><a id="__codelineno-107-5" name="__codelineno-107-5" href="#__codelineno-107-5"></a><span class="cp">endif</span>
</span></code></pre></div>
<h4 id="info">info<a class="headerlink" href="#info" title="Permanent link">&para;</a></h4>
<p>输出一些信息</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-108-1"><a id="__codelineno-108-1" name="__codelineno-108-1" href="#__codelineno-108-1"></a><span class="k">$(</span><span class="nv">info</span> <span class="nv">text</span>…<span class="k">)</span>
</span><span id="__span-108-2"><a id="__codelineno-108-2" name="__codelineno-108-2" href="#__codelineno-108-2"></a>
</span><span id="__span-108-3"><a id="__codelineno-108-3" name="__codelineno-108-3" href="#__codelineno-108-3"></a><span class="k">$(</span><span class="nv">info</span> <span class="nv">编译开始.......</span><span class="k">)</span>
</span><span id="__span-108-4"><a id="__codelineno-108-4" name="__codelineno-108-4" href="#__codelineno-108-4"></a><span class="k">$(</span><span class="nv">info</span> <span class="nv">编译结束</span><span class="k">)</span>
</span></code></pre></div>
<h1 id="makefile_7">同一项目中有多个Makefile文件<a class="headerlink" href="#makefile_7" title="Permanent link">&para;</a></h1>
<h2 id="makefile_8">包含其他makefile文件<a class="headerlink" href="#makefile_8" title="Permanent link">&para;</a></h2>
<p>使用<code>include</code>指令可以读入其他makefile文件的内容，效果就如同在include的位置用对应的文件内容替换一样。</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-109-1"><a id="__codelineno-109-1" name="__codelineno-109-1" href="#__codelineno-109-1"></a><span class="cp">include mkf1 mkf2 # 可以引入多个文件，用空格隔开</span>
</span><span id="__span-109-2"><a id="__codelineno-109-2" name="__codelineno-109-2" href="#__codelineno-109-2"></a><span class="cp">include *.mk    # 可以用通配符，表示引入所有以.mk结尾的文件</span>
</span></code></pre></div>
<p>如果找不到对应文件，则会报错，如果要忽略错误，可以在<code>include</code>前加<code>-</code></p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-110-1"><a id="__codelineno-110-1" name="__codelineno-110-1" href="#__codelineno-110-1"></a><span class="cp">-include mkf1 mkf2</span>
</span></code></pre></div>
<h4 id="_48">应用实例：自动生成依赖<a class="headerlink" href="#_48" title="Permanent link">&para;</a></h4>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-111-1"><a id="__codelineno-111-1" name="__codelineno-111-1" href="#__codelineno-111-1"></a><span class="nv">objs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>block.o<span class="w"> </span>command.o<span class="w"> </span>input.o<span class="w"> </span>main.o<span class="w"> </span>scene.o<span class="w"> </span>test.o
</span><span id="__span-111-2"><a id="__codelineno-111-2" name="__codelineno-111-2" href="#__codelineno-111-2"></a>
</span><span id="__span-111-3"><a id="__codelineno-111-3" name="__codelineno-111-3" href="#__codelineno-111-3"></a><span class="nf">sudoku</span><span class="o">:</span><span class="w"> </span><span class="k">$(</span><span class="nv">objs</span><span class="k">)</span>
</span><span id="__span-111-4"><a id="__codelineno-111-4" name="__codelineno-111-4" href="#__codelineno-111-4"></a><span class="w">    </span>g++<span class="w"> </span><span class="k">$(</span>objs<span class="k">)</span><span class="w"> </span>-o<span class="w"> </span>sudoku
</span><span id="__span-111-5"><a id="__codelineno-111-5" name="__codelineno-111-5" href="#__codelineno-111-5"></a>
</span><span id="__span-111-6"><a id="__codelineno-111-6" name="__codelineno-111-6" href="#__codelineno-111-6"></a><span class="cp">include $(objs:%.o=%.d)</span>
</span><span id="__span-111-7"><a id="__codelineno-111-7" name="__codelineno-111-7" href="#__codelineno-111-7"></a>
</span><span id="__span-111-8"><a id="__codelineno-111-8" name="__codelineno-111-8" href="#__codelineno-111-8"></a><span class="nf">%.d</span><span class="o">:</span><span class="w"> </span>%.<span class="n">cpp</span>
</span><span id="__span-111-9"><a id="__codelineno-111-9" name="__codelineno-111-9" href="#__codelineno-111-9"></a><span class="w">    </span>@-rm<span class="w"> </span><span class="nv">$@</span>
</span><span id="__span-111-10"><a id="__codelineno-111-10" name="__codelineno-111-10" href="#__codelineno-111-10"></a><span class="w">    </span><span class="k">$(</span>CXX<span class="k">)</span><span class="w"> </span>-MM<span class="w">  </span>$&lt;<span class="w"> </span>&gt;<span class="w"> </span><span class="nv">$@</span>.temp
</span><span id="__span-111-11"><a id="__codelineno-111-11" name="__codelineno-111-11" href="#__codelineno-111-11"></a><span class="w">    </span>@sed<span class="w"> </span><span class="s1">&#39;s,\($*\)\.o[ :]*,\1.o $@ : ,g&#39;</span><span class="w"> </span>&lt;<span class="w"> </span><span class="nv">$@</span>.temp<span class="w"> </span>&gt;<span class="w"> </span><span class="nv">$@</span>
</span><span id="__span-111-12"><a id="__codelineno-111-12" name="__codelineno-111-12" href="#__codelineno-111-12"></a><span class="w">    </span>@-rm<span class="w"> </span><span class="nv">$@</span>.temp
</span><span id="__span-111-13"><a id="__codelineno-111-13" name="__codelineno-111-13" href="#__codelineno-111-13"></a>
</span><span id="__span-111-14"><a id="__codelineno-111-14" name="__codelineno-111-14" href="#__codelineno-111-14"></a>
</span><span id="__span-111-15"><a id="__codelineno-111-15" name="__codelineno-111-15" href="#__codelineno-111-15"></a><span class="nf">%.o </span><span class="o">:</span><span class="w"> </span>%.<span class="n">cpp</span>
</span><span id="__span-111-16"><a id="__codelineno-111-16" name="__codelineno-111-16" href="#__codelineno-111-16"></a><span class="w">    </span>g++<span class="w"> </span>-c<span class="w"> </span>$&lt;
</span><span id="__span-111-17"><a id="__codelineno-111-17" name="__codelineno-111-17" href="#__codelineno-111-17"></a><span class="w">    </span>@echo<span class="w"> </span>$^
</span></code></pre></div>
<h2 id="make_2">嵌套make<a class="headerlink" href="#make_2" title="Permanent link">&para;</a></h2>
<p>如果将一个大项目分为许多小项目，则可以使用嵌套（递归）使用make。具体做法为，写一个总的Makefile，然后在每个子项目中都写一个Makefile，在总Makefile中进行调用。</p>
<p>例如，可以把sudoku项目中除main.cpp，test.cpp外的其他cpp存为一个子项目，编译为一个库文件，main.cpp test.cpp为另一个子项目，编译为.o然后链接库文件成可执行文件：</p>
<p>库文件Makefile</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-112-1"><a id="__codelineno-112-1" name="__codelineno-112-1" href="#__codelineno-112-1"></a><span class="cp">vpath %.h ../include</span>
</span><span id="__span-112-2"><a id="__codelineno-112-2" name="__codelineno-112-2" href="#__codelineno-112-2"></a>
</span><span id="__span-112-3"><a id="__codelineno-112-3" name="__codelineno-112-3" href="#__codelineno-112-3"></a><span class="nv">CXXFLAGS</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span>-I../include<span class="w"> </span>-fexec-charset<span class="o">=</span>GBK<span class="w"> </span>-finput-charset<span class="o">=</span>UTF-8
</span><span id="__span-112-4"><a id="__codelineno-112-4" name="__codelineno-112-4" href="#__codelineno-112-4"></a>
</span><span id="__span-112-5"><a id="__codelineno-112-5" name="__codelineno-112-5" href="#__codelineno-112-5"></a><span class="nv">cpps</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$(</span>wildcard<span class="w"> </span>*.cpp<span class="k">)</span>
</span><span id="__span-112-6"><a id="__codelineno-112-6" name="__codelineno-112-6" href="#__codelineno-112-6"></a><span class="nv">objs</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">$(</span>cpps:%.cpp<span class="o">=</span>%.o<span class="k">)</span>
</span><span id="__span-112-7"><a id="__codelineno-112-7" name="__codelineno-112-7" href="#__codelineno-112-7"></a>
</span><span id="__span-112-8"><a id="__codelineno-112-8" name="__codelineno-112-8" href="#__codelineno-112-8"></a><span class="nf">libsudoku.a</span><span class="o">:</span><span class="w"> </span><span class="k">$(</span><span class="nv">objs</span><span class="k">)</span>
</span><span id="__span-112-9"><a id="__codelineno-112-9" name="__codelineno-112-9" href="#__codelineno-112-9"></a><span class="w">    </span>ar<span class="w"> </span>rcs<span class="w"> </span><span class="nv">$@</span><span class="w"> </span>$^
</span><span id="__span-112-10"><a id="__codelineno-112-10" name="__codelineno-112-10" href="#__codelineno-112-10"></a>
</span><span id="__span-112-11"><a id="__codelineno-112-11" name="__codelineno-112-11" href="#__codelineno-112-11"></a><span class="nf">$(objs)</span><span class="o">:</span><span class="w"> </span>%.<span class="n">o</span> : %.<span class="n">cpp</span> %.<span class="n">h</span>
</span></code></pre></div>
<p>main.cpp test.cpp的Makefile</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-113-1"><a id="__codelineno-113-1" name="__codelineno-113-1" href="#__codelineno-113-1"></a><span class="nv">CXXFLAGS</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span>-I../include<span class="w"> </span>-fexec-charset<span class="o">=</span>GBK<span class="w"> </span>-finput-charset<span class="o">=</span>UTF-8
</span><span id="__span-113-2"><a id="__codelineno-113-2" name="__codelineno-113-2" href="#__codelineno-113-2"></a><span class="cp">vpath %.h ../include</span>
</span><span id="__span-113-3"><a id="__codelineno-113-3" name="__codelineno-113-3" href="#__codelineno-113-3"></a><span class="cp">vpath %.a ../lib</span>
</span><span id="__span-113-4"><a id="__codelineno-113-4" name="__codelineno-113-4" href="#__codelineno-113-4"></a>
</span><span id="__span-113-5"><a id="__codelineno-113-5" name="__codelineno-113-5" href="#__codelineno-113-5"></a><span class="nf">../sudoku</span><span class="o">:</span><span class="w"> </span><span class="n">main</span>.<span class="n">o</span> <span class="n">test</span>.<span class="n">o</span> -<span class="n">lsudoku</span>
</span><span id="__span-113-6"><a id="__codelineno-113-6" name="__codelineno-113-6" href="#__codelineno-113-6"></a><span class="w">    </span><span class="k">$(</span>CXX<span class="k">)</span><span class="w"> </span>-o<span class="w"> </span><span class="nv">$@</span><span class="w"> </span>$^
</span></code></pre></div>
<p>总的Makefile</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-114-1"><a id="__codelineno-114-1" name="__codelineno-114-1" href="#__codelineno-114-1"></a><span class="nf">.PHONY</span><span class="o">:</span><span class="w"> </span><span class="n">all</span> <span class="n">clean</span>
</span><span id="__span-114-2"><a id="__codelineno-114-2" name="__codelineno-114-2" href="#__codelineno-114-2"></a>
</span><span id="__span-114-3"><a id="__codelineno-114-3" name="__codelineno-114-3" href="#__codelineno-114-3"></a><span class="nf">all</span><span class="o">:</span><span class="w"> </span><span class="n">subsrc</span>
</span><span id="__span-114-4"><a id="__codelineno-114-4" name="__codelineno-114-4" href="#__codelineno-114-4"></a>
</span><span id="__span-114-5"><a id="__codelineno-114-5" name="__codelineno-114-5" href="#__codelineno-114-5"></a><span class="nf">subsrc</span><span class="o">:</span><span class="w"> </span><span class="n">sublib</span>
</span><span id="__span-114-6"><a id="__codelineno-114-6" name="__codelineno-114-6" href="#__codelineno-114-6"></a><span class="w">    </span><span class="k">$(</span>MAKE<span class="k">)</span><span class="w"> </span>-C<span class="w"> </span>src
</span><span id="__span-114-7"><a id="__codelineno-114-7" name="__codelineno-114-7" href="#__codelineno-114-7"></a>
</span><span id="__span-114-8"><a id="__codelineno-114-8" name="__codelineno-114-8" href="#__codelineno-114-8"></a><span class="nf">sublib</span><span class="o">:</span>
</span><span id="__span-114-9"><a id="__codelineno-114-9" name="__codelineno-114-9" href="#__codelineno-114-9"></a><span class="w">    </span><span class="k">$(</span>MAKE<span class="k">)</span><span class="w"> </span>-C<span class="w"> </span>lib
</span><span id="__span-114-10"><a id="__codelineno-114-10" name="__codelineno-114-10" href="#__codelineno-114-10"></a>
</span><span id="__span-114-11"><a id="__codelineno-114-11" name="__codelineno-114-11" href="#__codelineno-114-11"></a><span class="nf">clean</span><span class="o">:</span>
</span><span id="__span-114-12"><a id="__codelineno-114-12" name="__codelineno-114-12" href="#__codelineno-114-12"></a><span class="w">    </span>-rm<span class="w"> </span>*.exe<span class="w"> </span>src/*.o<span class="w"> </span>lib/*.o<span class="w"> </span>lib/*.a<span class="w"> </span>
</span></code></pre></div>
<p>其中</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-115-1"><a id="__codelineno-115-1" name="__codelineno-115-1" href="#__codelineno-115-1"></a><span class="k">$(</span><span class="nv">MAKE</span><span class="k">)</span><span class="w"> </span><span class="err">-C</span><span class="w"> </span><span class="err">subdir</span>
</span></code></pre></div>
<p>这一指令会自动进入subdir文件夹然后执行make。</p>
<p>可以通过<code>export</code>指令向子项目的make传递变量。</p>
<div class="language-makefile highlight"><pre><span></span><code><span id="__span-116-1"><a id="__codelineno-116-1" name="__codelineno-116-1" href="#__codelineno-116-1"></a><span class="k">export </span><span class="err">var</span><span class="w">  </span><span class="c"># 传递var</span>
</span><span id="__span-116-2"><a id="__codelineno-116-2" name="__codelineno-116-2" href="#__codelineno-116-2"></a><span class="k">export      </span><span class="c"># 传递所有变量</span>
</span><span id="__span-116-3"><a id="__codelineno-116-3" name="__codelineno-116-3" href="#__codelineno-116-3"></a><span class="k">unexport    </span><span class="c"># 取消传递</span>
</span></code></pre></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h1 id="_49">后续学习过程<a class="headerlink" href="#_49" title="Permanent link">&para;</a></h1>
<p>读一些开源项目的Makefile</p>
<p><strong>redis</strong>:<a href="https://github.com/redis/redis">https://github.com/redis/redis</a></p>
<p><strong>ffmpeg</strong>:<a href="https://github.com/FFmpeg/FFmpeg">https://github.com/FFmpeg/FFmpeg</a></p>
<p><strong>aubio</strong>:<a href="https://github.com/aubio/aubio">https://github.com/aubio/aubio</a></p>
<p><strong>libav</strong>:<a href="https://github.com/libav/libav">https://github.com/libav/libav</a></p>
<p><strong>OpenH264</strong>:<a href="https://github.com/cisco/openh264">https://github.com/cisco/openh264</a></p>
<p><strong>TinyVM</strong>:<a href="https://github.com/jakogut/tinyvm">https://github.com/jakogut/tinyvm</a></p>
<p><strong>TinyXML2</strong>:<a href="https://github.com/leethomason/tinyxml2">https://github.com/leethomason/tinyxml2</a></p>
<h1 id="_50">备注<a class="headerlink" href="#_50" title="Permanent link">&para;</a></h1>
<p>转载自  <a href="https://gitee.com/yanmu_ym/cpp">https://gitee.com/yanmu_ym/cpp</a></p>





                
              </article>
            </div>
          
          
        </div>
        
          <button type="button" class="md-top md-icon" data-md-component="top" hidden>
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8v12Z"/></svg>
            回到页面顶部
          </button>
        
      </main>
      
        <footer class="md-footer">
  
    
      
      <nav class="md-footer__inner md-grid" aria-label="页脚" >
        
          
          <a href="../PDF%E5%88%92%E8%AF%8D%E7%BF%BB%E8%AF%91/" class="md-footer__link md-footer__link--prev" aria-label="上一页: PDF划词翻译" rel="prev">
            <div class="md-footer__button md-icon">
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
            </div>
            <div class="md-footer__title">
              <span class="md-footer__direction">
                上一页
              </span>
              <div class="md-ellipsis">
                PDF划词翻译
              </div>
            </div>
          </a>
        
        
          
          <a href="../RT_Smart%E4%B8%ADminizip%E7%A7%BB%E6%A4%8D%E8%AE%B0%E5%BD%95/" class="md-footer__link md-footer__link--next" aria-label="下一页: RT_Smart中minizip移植记录" rel="next">
            <div class="md-footer__title">
              <span class="md-footer__direction">
                下一页
              </span>
              <div class="md-ellipsis">
                RT_Smart中minizip移植记录
              </div>
            </div>
            <div class="md-footer__button md-icon">
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
            </div>
          </a>
        
      </nav>
    
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    
    <script id="__config" type="application/json">{"base": "../..", "features": ["navigation.tabs", "navigation.top", "navigation.footer", "content.code.copy", "content.code.select", "content.code.annotate"], "search": "../../assets/javascripts/workers/search.208ed371.min.js", "translations": {"clipboard.copied": "\u5df2\u590d\u5236", "clipboard.copy": "\u590d\u5236", "search.result.more.one": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.more.other": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 # \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.none": "\u6ca1\u6709\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.one": "\u627e\u5230 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.other": "# \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.placeholder": "\u952e\u5165\u4ee5\u5f00\u59cb\u641c\u7d22", "search.result.term.missing": "\u7f3a\u5c11", "select.version": "\u9009\u62e9\u5f53\u524d\u7248\u672c"}}</script>
    
    
      <script src="../../assets/javascripts/bundle.b4d07000.min.js"></script>
      
        <script src="../../js/extra.js"></script>
      
        <script src="../../js/baidu-tongji.js"></script>
      
        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML"></script>
      
    
  </body>
</html>